Open darkprinx opened 5 years ago
Solution Can be of question 14: string=input("Enter the sentense") upper=0 lower=0 for x in string: if x.isupper()==True: upper+=1 if x.islower()==True: lower+=1
print("UPPER CASE: ",upper) print("LOWER CASE: ",lower)
Solution Can be of question 14: string=input("Enter the sentense") upper=0 lower=0 for x in string: if x.isupper()==True: upper+=1 if x.islower()==True: lower+=1
print("UPPER CASE: ",upper) print("LOWER CASE: ",lower)
Added. Thank you :)
In problem 17 How do I terminate this while true condition?????
Just check whether the input is empty or not.
In answer 14, your solution 2, what is '{0}' and '{1}'? I don't understand.
In python, while printing a dynamic value inside a string, there are several ways to format the output string. For example, let's say you have two variables a = 10 and b = 20 and you want to output something like this,
The sum of 10 and 20 is 30
You can do this by writing it in this way,
print("The sum of {0} and {1} is {2}".format(a, b, a + b))
Here 0, 1, 2 inside '{ }' represents the order of a, b and a+b respectively.
However, If the code is written in this way,
print("The sum of {1} and {0} is {2}".format(a, b, a + b))
then the output will be like,
The sum of 20 and 10 is 30
But printing in this way is not very much necessary. It can be also written in this way,
print("The sum of {} and {} is {}".format(a, b, a + b))
which will give the same output as wanted. I was new to python at that timeline so that I learned and used in {0}, {1} style which is actually not mandatory at all.
Thank you :)
Thank you for teaching me.
x=input().split(",") ans=list(filter(lambda x : int(x)%2!=0 ,x)) print(",".join(ans))
This could be the ans for 16
x=input().split(",") ans=list(filter(lambda x : int(x)%2!=0 ,x)) print(",".join(ans))
Added to question# 16
Question 38 ans proposition:
tup = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10) lt = int(len(tup)/2) print(tup[:lt], tup[lt:])
Question 38 ans proposition:
tup = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10) lt = int(len(tup)/2) print(tup[:lt], tup[lt:])
Added. Thank you.
For Q. 16 the answers are all wrong.
You say square each odd number yet your demo answer just prints odd numbers
The correct answer is:
num = input("Enter numbers: ").split(',')
print (",".join([str(int(x)**2) for x in num if int(x)%2 != 0]))
For Q. 16 the answers are all wrong.
You say square each odd number yet your demo answer just prints odd numbers
The correct answer is:
num = input("Enter numbers: ").split(',') print (",".join([str(int(x)**2) for x in num if int(x)%2 != 0]))
Yes, I also noticed the mistake. It's now fixed. Thank you for the cooperation :)
Hi! I don't completely understand how the solution for question 9 works. Why is it a list what is created? Also, when I run the solution on my computer, the result is not capitalized.
Hi! I don't completely understand how the solution for question 9 works. Why is it a list what is created? Also, when I run the solution on my computer, the result is not capitalized.
Hi Leonedott, I did a little mistake in the given code for python3. Thus, the result wasn't showing to you as expected. The code has been fixed. Now I'm explaining how the solution works.
As the question says, a program that accepts sequence of lines that means the output will come after all the sequence of lines are taken. That's why a list is created to save the processed inputs and then show them all together as output.
The program will take input until there is an empty line. The program will take a line as input, then make it upper case and append in the list. If the program founds any empty string line, it will break and come out of the loop, and then show all the lines which are saved in the list.
If you still have any queries, please feel free to ask. Thank you :)
Here's my solution to exercise 17
`lst = [] while True: x = input(': ') if len(x)==0: break; lst.append(x)
balance = 0 for item in lst: if 'D' in item: balance += int(item.strip('D ')) if 'W' in item: balance -= int(item.strip('W ')) print(balance)`
Thanks so much for these exercises @darkprinx !!!
Here's my solution to exercise 17
`lst = [] while True: x = input(': ') if len(x)==0: break; lst.append(x)
balance = 0 for item in lst: if 'D' in item: balance += int(item.strip('D ')) if 'W' in item: balance -= int(item.strip('W ')) print(balance)`
Thanks so much for these exercises @darkprinx !!!
Thanks for sharing your solution. There was a little mistake. I corrected it and added it.
Who is the main author plzz telll me sir,@darkprinx
Problem with Question 70. It says random even integer from 0 to 10. 0 is even. But they left it out.
Who is the main author plzz telll me sir,@darkprinx
I actually mentioned where I get these problems. It's all described here in the readme. https://github.com/darkprinx/100-plus-Python-programming-exercises-extended
I tend to mention the person as the main author coz the source I found first from his repository.
Problem with Question 70. It says random even integer from 0 to 10. 0 is even. But they left it out.
It was a mistake in my code of python3. I mistakenly ignored 0 as a member of the list. I fixed it now. Thank you very much for the co-operation :-)
Can you please give me more question from topics
On Wed, 6 Nov 2019, 11:22 p.m. MarkisLandis <notifications@github.com wrote:
Problem with Question 70. It says random even integer from 0 to 10. 0 is even. But they left it out.
— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/darkprinx/100-plus-Python-programming-exercises-extended/issues/3?email_source=notifications&email_token=ANMX5BM77LHPDDZTHLMYFALQSL74VA5CNFSM4GQP6TLKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEDHNHEI#issuecomment-550425489, or unsubscribe https://github.com/notifications/unsubscribe-auth/ANMX5BMDAPR2DRK22XJVDJLQSL74VANCNFSM4GQP6TLA .
Answer for Question #30 func = lambda a,b: print(max((a,b),key=len)) if len(a)!=len(b) else print(a+'\n'+b)
Question #34 answer func = lambda :print([i**2 for i in range(1,21)][:5])
Question #34 answer func = lambda :print([i**2 for i in range(1,21)][:5])
Both solutions are added. Thank you :)
Question #60 The problem statement should be f(0) = 0, otherwise f(5)=501
Question #67 Recursion method
idx = 0 def bs(num,num_list): global idx if (len(num_list) == 1): if num_list[0] == num: return idx else: return "No exit in the list" elif num in num_list[:len(num_list)//2]: return bs(num,num_list[:len(num_list)//2]) else: idx += len(num_list)//2 return bs(num,num_list[len(num_list)//2:]) print(bs(5,[1,5,8,10,12,13,55,66,73,78,82,85,88,99,100]))
@yuan1z Contributions are added. Thank you. :)
Question 2, Method 3 (recursive function) needs a little correction because in case if input 0 the function throw an error but 0! is defined as 1: The solution is return 1 if x less or equal to 1:
`n = int(input())
def shortFact(x): return 1 if x <= 1 else x*shortFact(x-1)
print(shortFact(n))`
@StartZer0 The bug has been fixed. Thanks for your cooperation :)
Question #88 answer
my_list = [12,24,35,24,88,120,155,88,120,155] print(sorted(list(set(my_list))))
Question 101 answer using set and list.count X = input() my_set = set(X) arr = [] for item in my_set: arr.append([item,X.count(item)]) tmp = sorted(arr,key = lambda x: (-x[1],x[0])) for i in tmp[:3]: print(i[0]+' '+str(i[1]))
@yuan1z , the solution for #88 will fail for an the input [155, 120, 88, 155, 88, 120, 24, 35, 24, 12] as the question mentioned to reserve the order of input.
Solution for #101 is added. Thank you :)
just another simple solution for question 63, if there is no requirement to use yield:
n = int(input())
for i in range(0, n+1, 2):
if i < n - 1:
print(i, end = ',' )
else:
print(i)
I think this is not necessary to add another solution for question 101 as the solution is already given, but anyway I shared:
s = list(input())
dict_count_ = {k:s.count(k) for k in s}
list_of_tuples = [(k,v) for k,v in dict_count_.items()]
list_of_tuples.sort(key = lambda x: x[1], reverse = True)
for item in list_of_tuples:
print(item[0], item[1])
Question 17 upgraded :)
account = 0
while True:
action = input("Deposit/Whitdrow/Balance/Quit? D/W/B/Q: ").lower()
if action == "d":
deposit = input("How much would you like to deposit? ")
account = account + int(deposit)
elif action == "w":
withdrow = input("How much would you like to withdrow? ")
account = account - int(withdrow)
elif action == "b":
print(account)
else:
quit()
Question #6 that's my solution for the exercise. i'd like to ask why, if i write return insted of print inside the definition of the function it returns only the last result (i.e. the result with the last number inserted) import math def formula (C=50,H=30,*args):
args = input('insert number: ').split(',')
for i in args:
Q = math.sqrt((2*C*int(i))/H)
print(round(Q))
formula()
@StartZer0 @AlexanderSro All the solutions provided are added. Thank you very much for the contribution :)
@simonetrav Thanks for your query. I have shared an explaination with your code,
def formula (C=50,H=30,*args):
args = input('insert number: ').split(',')
for i in args:
Q = math.sqrt((2*C*int(i))/H)
return Q # <---- if you put return here, then the function will return the value of Q driven from first number.
# The loop will not further continue for all the value given as input.
def formula (C=50,H=30,*args):
args = input('insert number: ').split(',')
for i in args:
Q = math.sqrt((2*C*int(i))/H)
return Q # <---- if you put return here, then the function will return the value of Q driven from the latest number.
# It's because the value of Q always changed and updated inside the loop. As the last update of Q
# occurs for the last given input, thus the output is also associated with the last input.
I don't agree with Question #20. You are asking for a generator, but your answer provides a list. Shouldn't the solution be more like this?:
class Divisible:
def by_seven(self, n):
for number in range(n + 1):
if number % 7 == 0: yield number
divisible = Divisible()
generator = divisible.by_seven(int(input("Please insert a number. --> ")))
for number in generator:
print(number)
Solution to Question 95: num = int(input("Enter num: ")) L = [] while True: L.append(num) num = int(input("Enter another: "))
if num == 0:
break
print(L)
L1 =set(L)
print(f'The runner up is {max(L1)-1}')
Solution to Question 96: import textwrap
string = input() width = int(input())
print(textwrap.fill(string,width))
@mishrasunny-coder, hello there. Thanks for the solution you tried to contribute. Unfortunately, your solution for problem #95 fails for the test case like [1, 4, 9, 6] where the expected output should be 6 but your solution gives 8. Please review the solution and submit again.
The solution to problem #96 is added. Thanks for your contribution :)
I don't agree with Question #20. You are asking for a generator, but your answer provides a list. Shouldn't the solution be more like this?:
class Divisible: def by_seven(self, n): for number in range(n + 1): if number % 7 == 0: yield number divisible = Divisible() generator = divisible.by_seven(int(input("Please insert a number. --> "))) for number in generator: print(number)
Yes, I just saw the problem and ensured it was my mistake at that time. I usually didn't know that the generator indicates something else in python when I inserted it. I have replaced my solution with your one. Thank you very much for correcting and contributing your solution.
UPDATED SOLUTION to QUESTION 95:
num = int(input("Enter num: ")) L = [] while True: L.append(num) num = int(input("Enter another: "))
if num == 0:
break
L1 = list(set(L[:])) L2 = sorted(L1)
print(f'The runner up is {L2[-2]}')
@mishrasunny-coder, hello there. Thanks for the solution you tried to contribute. Unfortunately, your solution for problem #95 fails for the test case like [1, 4, 9, 6] where the expected output should be 6 but your solution gives 8. Please review the solution and submit again.
The solution to problem #96 is added. Thanks for your contribution :)
Thanks so much :) I have added the updated solution to Question 95. Thanks for catching the error.
@mishrasunny-coder your updated code has been added to the solution. Thank you :)
Very Easy Solution to Question 22: L = input().split() L1 = {i:L.count(i) for i in L} print(f'the count is {L1}')
In Question 37 it seems unnecessary to make a list first. I suggest this:
def square_of_numbers():
return tuple(i ** 2 for i in range(1, 21))
print(square_of_numbers())
Question #40 asks to return "Yes" only if the input was "yes" or "YES" or "Yes". With your solution things like "yEs", "yeS" and YeS etc. will all print "Yes" which is not what the question is asking for. The solution by AasaiAlangaram that was added, does work, but i am not sure that naming a variable the same thing as a built in function is right (input). in this case it might not matter, but if you later want to use the input function, you actually can't. Not sure, if this is a training tool, that teaching things like that are very beneficial.
A normal python 2 to python 3 conversion would in my opinion be a lot better than both given python 3 solutions.
text = input("Please type something. --> ")
if text == "yes" or text == "YES" or text == "Yes":
print("Yes")
else:
print("No")
Hello Guys
If anyone wants to -
Please feel free to write it down and discuss here.
Sharing new questions and solutions are warmly welcome. Be a proud contributor of this repository by just making a pull request of your changes.