Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,150,852 members, 7,810,280 topics. Date: Saturday, 27 April 2024 at 04:49 AM

Python 3: Exercises And Solutions. - Programming (6) - Nairaland

Nairaland Forum / Science/Technology / Programming / Python 3: Exercises And Solutions. (8867 Views)

Salesforce Rest API Example Using Python 3 / Consume Kibana Rest API Using Python 3 / How To Write A Guessing Game Program In Python 3 (2) (3) (4)

(1) (2) (3) (4) (5) (6) (Reply) (Go Down)

Re: Python 3: Exercises And Solutions. by iCode2: 2:07pm On Jun 10, 2020
Write a program that asks the user to enter a sentence and then randomly rearranges the words of the sentence. Make sure that the sentence that will be printed out starts with a capital, that the original first word is not capitalised if it comes in the middle of the sentence, and that the period is in the right place.
Re: Python 3: Exercises And Solutions. by gbolly1151(m): 7:02pm On Jun 11, 2020
iCode2:
Write a program that asks the user to enter a sentence and then randomly rearranges the words of the sentence. Make sure that the sentence that will be printed out starts with a capital, that the original first word is not capitalised if it comes in the middle of the sentence, and that the period is in the right place.
Can you cite example out input and output?
Re: Python 3: Exercises And Solutions. by gbolly1151(m): 7:06pm On Jun 11, 2020
Question to check your data structure and algorithm skill

When a share of common stock of some company is sold, the capital gain (or, sometimes, loss) is the difference between the share’s selling price and the price originally paid to buy it. This rule is easy to understand for a single share, but if we sell multiple shares of stock bought over a long period of time, then we must identify the shares actually being sold. A standard accounting principle for identifying which shares of a stock were sold in such a case is to use a FIFO protocol—the shares sold are the ones that have been held the longest (indeed, this is the default method built into several personal finance software packages). For example, suppose we buy 100 shares at $20 each on day 1, 20 shares at $24 on day 2, 200 shares at $36 on day 3, and then sell 150 shares on day 4 at $30 each. Then applying the FIFO protocol means that of the 150 shares sold, 100 were bought on day 1, 20 were bought on day 2, and 30 were bought on day 3. The capital gain in this case would therefore be 100·10+20·6+30·(−6), or $940. Write a program that takes as input a sequence of transactions of the form “buy x share(s) at $y each” or “sell x share(s) at $y each,” assuming that the transactions occur on consecutive days and the values x and y are integers. Given this input sequence, the output should be the total capital gain (or loss) for the entire sequence, using the FIFO protocol to identify shares.
Re: Python 3: Exercises And Solutions. by Brukx(m): 7:25pm On Jun 11, 2020
iCode2:
Write a program that asks the user to enter a sentence and then randomly rearranges the words of the sentence. Make sure that the sentence that will be printed out starts with a capital, that the original first word is not capitalised if it comes in the middle of the sentence, and that the period is in the right place.


import random
def main() :
sentence=input("enter a sentence: " )
list=sentence.split(" " )
new_sentence= random.shuffle(list)
new_sentence.capitalize(0)
new_sentence.replace(.,""wink
new_sentence(len(new_sentence))+("." )
print (', '.join(new_sentence))

main()
Re: Python 3: Exercises And Solutions. by iCode2: 8:09pm On Jun 11, 2020
gbolly1151:

Can you cite example out input and output?
Input: This boy is a love child of a love God.

Output could be: Child god love this a boy is love a of.
Re: Python 3: Exercises And Solutions. by iCode2: 8:13pm On Jun 11, 2020
Brukx:



import random
def main() :
sentence=input("enter a sentence: " )
list=sentence.split(" " )
new_sentence= random.shuffle(list)
new_sentence.capitalise(0)
new_sentence.replace(.)
new_sentence(len(new_sentence))+("." )
print (', '.join(new_sentence))

main()
I got a syntax error from line 7.
Re: Python 3: Exercises And Solutions. by gbolly1151(m): 11:43pm On Jun 11, 2020
iCode2:
Write a program that asks the user to enter a sentence and then randomly rearranges the words of the sentence. Make sure that the sentence that will be printed out starts with a capital, that the original first word is not capitalised if it comes in the middle of the sentence, and that the period is in the right place.


sentence=str(input('your sentence: ')).replace('.','').split()
random.shuffle(sentence)
print(" ".join(sentence).capitalize(),end='.')


please check
Re: Python 3: Exercises And Solutions. by iCode2: 11:54pm On Jun 11, 2020
gbolly1151:



sentence=str(input('your sentence: ')).split()
random.shuffle(sentence)
print(" ".join(sentence).capitalize(),end='.')


please check
Wow it worked! Thanks man. I didn't know about that capitalise method. You're good man.
Re: Python 3: Exercises And Solutions. by gbolly1151(m): 12:02am On Jun 12, 2020
iCode2:
Wow it worked! Thanks man. I didn't know about that capitalise method. You're good man.
There is a bug that i fix just now,i remove initial (.) From the sentence to avoid two (.)
Re: Python 3: Exercises And Solutions. by Brukx(m): 12:25am On Jun 12, 2020
iCode2:
I got a syntax error from line 7.
I've modified it. Check it again
Re: Python 3: Exercises And Solutions. by iCode2: 7:07am On Jun 12, 2020
Brukx:



import random
def main() :
sentence=input("enter a sentence: " )
list=sentence.split(" " )
new_sentence= random.shuffle(list)
new_sentence.capitalize(0)
new_sentence.replace(.,""wink
new_sentence(len(new_sentence))+("." )
print (', '.join(new_sentence))

main()
Same thing sir.
Re: Python 3: Exercises And Solutions. by iCode2: 7:11am On Jun 12, 2020
gbolly1151:



sentence=str(input('your sentence: ')).replace('.','').split()
random.shuffle(sentence)
print(" ".join(sentence).capitalize(),end='.')


please check
Still worked.
gbolly1151:

There is a bug that i fix just now,i remove initial (.) From the sentence to avoid two (.)
I didn't notice the bug with the first one though.
Re: Python 3: Exercises And Solutions. by DAramis: 11:59am On Jun 30, 2020
Hello bosses and Madams,
Someone should help me out in this python program (I justed started learning it but was stuck in this assignment). The question is in red while my answer is in blue.

Who are you?
I'm fine, thanks. Who are you?
Who are you?
Joe
Hello, Joe. What is the password? (It is a fish.)
Mary
Who are you?
Joe
Hello, Joe. What is the password? (It is a fish.)
swordfish
Access granted.


My code (though not running)

name = ''
While not name:
print ('Who are you?')
name = input ()
while name != 'Joe':
print ('Who are you?')
break
print ('Hello Joe. What is the password?' (it is fish))
password = input ()
while password != 'swordfish'
print ('Hello Joe. what is the password?' (it is fish))
break
print ('Access granted')
Re: Python 3: Exercises And Solutions. by Shepherdd(m): 12:12pm On Jun 30, 2020
DAramis:
Hello bosses and Madams,
Someone should help me out in this python program (I justed started learning it but was stuck in this assignment). The question is in red while my answer is in blue.

Who are you?
I'm fine, thanks. Who are you?
Who are you?
Joe
Hello, Joe. What is the password? (It is a fish.)
Mary
Who are you?
Joe
Hello, Joe. What is the password? (It is a fish.)
swordfish
Access granted.


My code (though not running)

name = ''
While not name:
print ('Who are you?')
name = input ()
while name != 'Joe':
print ('Who are you?')
break
print ('Hello Joe. What is the password?' (it is fish))
password = input ()
while password != 'swordfish'
print ('Hello Joe. what is the password?' (it is fish))
break
print ('Access granted')

Your first two loops are redundant. Try and merge them into something like

secret_name = "Joe"
name = input('what is your name')

while name != secret_name:
name = input('what is your name')

Then do the same for the password. It's called sentinel controlled loop.

1 Like

Re: Python 3: Exercises And Solutions. by DAramis: 3:21pm On Jun 30, 2020
Shepherdd:


Your first two loops are redundant. Try and merge them into something like

secret_name = "Joe"
name = input('what is your name')

while name != secret_name:
name = input('what is your name')

Then do the same for the password. It's called sentinel controlled loop.
Thanks for the effort. I did it but didn't work. You can write the full code for me to have an idea if my request is not to much.
Below is the code:

secretName = 'Joe'
name = input ('What is your name?')
while name != secretName:
name = input ('What is your name?')
break
secretPassword = 'swordfish'
password input ('What is your password?')
while password != secretPassword:
password = input ('What is your password?')
Re: Python 3: Exercises And Solutions. by Shepherdd(m): 4:07pm On Jun 30, 2020
DAramis:

Thanks for the effort. I did it but didn't work. You can write the full code for me to have an idea if my request is not to much.
Below is the code:

secretName = 'Joe'
name = input ('What is your name?')
while name != secretName:
name = input ('What is your name?')
break
secretPassword = 'swordfish'
password input ('What is your password?')
while password != secretPassword:
password = input ('What is your password?')

secretName = 'Joe'
name = input ('What is your name?')
while name != secretName:
name = input ('What is your name?')

secretPassword = 'swordfish'
password = input (f'Hello, {secretName}. What is the password? (It is a fish.)')
while password != secretPassword:
password = input (f'Hello, {secretName}. What is the password? (It is a fish.')
Print('Acess granted')
Re: Python 3: Exercises And Solutions. by DAramis: 7:31pm On Jun 30, 2020
Shepherdd:


secretName = 'Joe'
name = input ('What is your name?')
while name != secretName:
name = input ('What is your name?')

secretPassword = 'swordfish'
password = input (f'Hello, {secretName}. What is the password? (It is a fish.)')
while password != secretPassword:
password = input (f'Hello, {secretName}. What is the password? (It is a fish.')
Print('Acess granted')

Thanks, would try it out tomorrow and give you feedback.

Spanish clasico is starting in few hours grin
Need to watch the match.
Re: Python 3: Exercises And Solutions. by DAramis: 9:21am On Jul 01, 2020
Shepherdd:


secretName = 'Joe'
name = input ('What is your name?')
while name != secretName:
name = input ('What is your name?')

secretPassword = 'swordfish'
password = input (f'Hello, {secretName}. What is the password? (It is a fish.)')
while password != secretPassword:
password = input (f'Hello, {secretName}. What is the password? (It is a fish.')
Print('Acess granted')
Copied it exactly like this and pasted on the IDP. It worked right now. Thanks.
Re: Python 3: Exercises And Solutions. by rastaxarm(m): 1:06am On Jul 02, 2020
A Voting System WebApp created with Python/Flask

Home Page: https://digitavote.pythonanywhere.com


VOTING LIVE VIEW: https://digitavote.pythonanywhere.com/vote/view/live/

Equipped with dashboard. Good for schools and organization elections

Still under development
cool

Re: Python 3: Exercises And Solutions. by NettyNelly(m): 7:59pm On Jul 12, 2020
gbolly1151:
Question to check your data structure and algorithm skill

When a share of common stock of some company is sold, the capital gain (or, sometimes, loss) is the difference between the share’s selling price and the price originally paid to buy it. This rule is easy to understand for a single share, but if we sell multiple shares of stock bought over a long period of time, then we must identify the shares actually being sold. A standard accounting principle for identifying which shares of a stock were sold in such a case is to use a FIFO protocol—the shares sold are the ones that have been held the longest (indeed, this is the default method built into several personal finance software packages). For example, suppose we buy 100 shares at $20 each on day 1, 20 shares at $24 on day 2, 200 shares at $36 on day 3, and then sell 150 shares on day 4 at $30 each. Then applying the FIFO protocol means that of the 150 shares sold, 100 were bought on day 1, 20 were bought on day 2, and 30 were bought on day 3. The capital gain in this case would therefore be 100·10+20·6+30·(−6), or $940. Write a program that takes as input a sequence of transactions of the form “buy x share(s) at $y each” or “sell x share(s) at $y each,” assuming that the transactions occur on consecutive days and the values x and y are integers. Given this input sequence, the output should be the total capital gain (or loss) for the entire sequence, using the FIFO protocol to identify shares.

What modules do I need to import to solve this.?
I used queue and random modules, but seems something is missing.
Bro, do share the solution with us. Seems none of us has a a good solution.
Re: Python 3: Exercises And Solutions. by gbolly1151(m): 11:10pm On Jul 12, 2020
NettyNelly:


What modules do I need to import to solve this.?
I used queue and random modules, but seems something is missing.
Bro, do share the solution with us. Seems none of us has a a good solution.

I will try and solve it and release the answer tomorrow
Re: Python 3: Exercises And Solutions. by gbolly1151(m): 11:13pm On Jul 12, 2020
NettyNelly:


What modules do I need to import to solve this.?
I used queue and random modules, but seems something is missing.
Bro, do share the solution with us. Seems none of us has a a good solution.
Queue is FIFO so you can use it,but the real data structure to use in real world should be Priority Queue
Re: Python 3: Exercises And Solutions. by gbolly1151(m): 12:24pm On Jul 13, 2020
NettyNelly:


What modules do I need to import to solve this.?
I used queue and random modules, but seems something is missing.
Bro, do share the solution with us. Seems none of us has a a good solution.

Here is what i solve long time ago and works


class share:
def __init__(self,unit,price):
self.unit=unit
self.price=price

class stock:
def __init__(self):
self.shares=[]
self.CGL=0
self.total_units=0

def buy(self,unit,price):
self.total_units += unit
self.shares.insert(0,share(unit,price))

def sell(self,unit,price):
assert unit < self.total_units, "units can't exceed total units"
while unit > 0:
share=self.shares.pop()
curr_share=0
if unit > share.unit:
curr_share=share.unit
unit -= share.unit
else:
curr_share=unit
share.unit -= curr_share
self.shares.append(share)
unit = 0
self.CGL += (curr_share * (price - share.price))
self.total_units -= curr_share



S1=stock()
buy=[(100,20),(20,24),(200,36)]
for unit,price in buy:
S1.buy(unit,price)
S1.sell(150,30)
print('capital gain is',S1.CGL,'\ntotal unit left',S1.total_units)



Re: Python 3: Exercises And Solutions. by Etinosa1234: 8:23pm On Jul 14, 2020
jayphe:


Exercise 1


Write a python program that obtains the domain name (thapar.edu) from the URL

http://www.thapar.edu/index.php/about-us/mission

I'm Learning Java so I Jus decided to use it to strengthen my grip on the language

package com.Package;

public class Exercises {
public static void main(String args[]){
//A program that extract the domain name
String web="www.thaparedu.url";
String site=web.substring(4,13);

System.out.println(site);
}
}
Re: Python 3: Exercises And Solutions. by Etinosa1234: 8:25pm On Jul 14, 2020
jayphe:

This is another exercise.
Exercise 2

Write a Python program that accepts a string/sentence from the user. The program then changes all spaces to underscores. Finally, it prints out the final result.

Good luck guys!
package com.Package;
import java.util.Scanner;

public class AnoExe {
public static void main(String args[]){
Scanner word=new Scanner(System.in);

System.out.println("How do u feel today"wink;
String reply=word.nextLine();
String replyunder=reply.replace(" ","_"wink;
System.out.println(replyunder);



}
}
Re: Python 3: Exercises And Solutions. by Etinosa1234: 8:30pm On Jul 14, 2020
jayphe:

Collinsanele Taofeekdboy kensmoney Daddyshome Zabiboy

hmm....who else ooo? Let's get to business. Pls, let's try to read the question in details first. If the question asks for the users' input, then we should include an input statement. Zabiboy we need to talk ooo. This one wey I no see you....


Exercise 4
We are doing some 'loop and looping stuffs' today.


Write a Python program that accepts the name of a student, matric number, course code and the score he/she got in the course. E.g
Name: John Alfred
Matric. No.: MUC/CS/014/0882
Course Code: CMP 212
Score: 100

The program then print the result as:
John Alfred, MUC/CS/014/0882, CMP 212, Grade A


Using this grading table.
100-95 A
94-85 B
84-70 C
69-60 D
59-50 E
49-0 F

Corrections, advices, suggestions and opinions are highly welcome!
I don't think looping is needed here

com.Package;
import java.util.Scanner;

public class Student {
public static void main(String args[]){
Scanner res=new Scanner(System.in);

System.out.println("What's your name"wink;
String name=res.nextLine();

System.out.println("Matric number"wink;
String Matric=res.nextLine();

System.out.println("What's your course"wink;
String course=res.nextLine();

System.out.println("What is ur score"wink;
int score=res.nextInt();

if (score > 95){
System.out.println("U got an A"wink;
}else if (score < 94&& score >85) {
System.out.println("U got a B"wink;
}else if(score <84&& score>75) {
System.out.println("U got a C"wink;
}else {
System.out.println("U tried ur best"wink;
}
System.out.println("Dear "+name+ " The course u did "+course+" resulted in the "+score+ " %score "wink;

}
}
Re: Python 3: Exercises And Solutions. by Etinosa1234: 8:33pm On Jul 14, 2020
jayphe:

Exercise 6
Write a Python program that accepts an integer number and checks if it is an even number or an odd number. Pls don't use the % (modulus) operator. Let's see some genuius.

package com.Package;
import java.util.Scanner;

public class EvenNum {
public static void main(String args []){
//to check for an even numbee
Scanner word=new Scanner(System.in);

System.out.println("Input a number from 100-1000"wink;
int num=word.nextInt();
int res=num%2;

if (res == 1){
System.out.println("It is an odd number"wink;
}else{
System.out.println("It is an even number "wink;
}
}
}
Re: Python 3: Exercises And Solutions. by Vecto(m): 2:43pm On Aug 26, 2021
yo what's the code for this?
Write a program that displays the number of seconds in a week.

(1) (2) (3) (4) (5) (6) (Reply)

Kehinde Adeyemi Among 12 Best Global IT Developers / Mobile Phone Apps For Nigeria / Why Is Programming Difficult?

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 54
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.