From 94f7540e37d56b7cce3d85c83d54d7056c307d6a Mon Sep 17 00:00:00 2001 From: Jakob Merz Date: Sun, 26 May 2019 21:53:48 +0200 Subject: [PATCH 01/23] Excercise 1 finished --- ...thon challenging programming exercises.txt | 72 +++++++++++-------- 1 file changed, 43 insertions(+), 29 deletions(-) diff --git a/100+ Python challenging programming exercises.txt b/100+ Python challenging programming exercises.txt index 97af5aaf..22ee182c 100644 --- a/100+ Python challenging programming exercises.txt +++ b/100+ Python challenging programming exercises.txt @@ -24,9 +24,23 @@ Write a program which will find all such numbers which are divisible by 7 but ar between 2000 and 3200 (both included). The numbers obtained should be printed in a comma-separated sequence on a single line. -Hints: +x = range(2000,3200) +x.each( + ) + +print(x) + +Hints: Consider use range(#begin, #end) method + + + + + + + + Solution: l=[] for i in range(2000, 3201): @@ -156,7 +170,7 @@ The output of the program should be: Hints: If the output received is in decimal form, it should be rounded off to its nearest value (for example, if the output received is 26.0, it should be printed as 26) -In case of input data being supplied to the question, it should be assumed to be a console input. +In case of input data being supplied to the question, it should be assumed to be a console input. Solution: #!/usr/bin/env python @@ -182,7 +196,7 @@ Example Suppose the following inputs are given to the program: 3,5 Then, the output of the program should be: -[[0, 0, 0, 0, 0], [0, 1, 2, 3, 4], [0, 2, 4, 6, 8]] +[[0, 0, 0, 0, 0], [0, 1, 2, 3, 4], [0, 2, 4, 6, 8]] Hints: Note: In case of input data being supplied to the question, it should be assumed to be a console input in a comma-separated form. @@ -616,7 +630,7 @@ Question 22 Level 3 Question: -Write a program to compute the frequency of the words from the input. The output should output after sorting the key alphanumerically. +Write a program to compute the frequency of the words from the input. The output should output after sorting the key alphanumerically. Suppose the following input is supplied to the program: New to Python or choosing between Python 2 and Python 3? Read Python 2 or Python 3. Then, the output should be: @@ -674,7 +688,7 @@ Question: Python has many built-in functions, and if you do not know how to use it, you can read document online or find some books. But Python has a built-in document function for every built-in functions. Please write a program to print some Python built-in functions documents, such as abs(), int(), raw_input() And add document for your own function - + Hints: The built-in document method is __doc__ @@ -685,7 +699,7 @@ print raw_input.__doc__ def square(num): '''Return the square value of the input number. - + The input number must be integer. ''' return num ** 2 @@ -709,7 +723,7 @@ Solution: class Person: # Define the class parameter "name" name = "Person" - + def __init__(self, name = None): # self.name is the instance parameter self.name = name @@ -748,7 +762,7 @@ def printValue(n): print str(n) printValue(3) - + #----------------------------------------# Question: @@ -820,7 +834,7 @@ def printValue(s1,s2): else: print s1 print s2 - + printValue("one","three") @@ -842,7 +856,7 @@ def checkValue(n): print "It is an even number" else: print "It is an odd number" - + checkValue(7) @@ -865,7 +879,7 @@ def printDict(): d[2]=2**2 d[3]=3**2 print d - + printDict() @@ -891,7 +905,7 @@ def printDict(): for i in range(1,21): d[i]=i**2 print d - + printDict() @@ -914,9 +928,9 @@ def printDict(): d=dict() for i in range(1,21): d[i]=i**2 - for (k,v) in d.items(): + for (k,v) in d.items(): print v - + printDict() @@ -938,9 +952,9 @@ def printDict(): d=dict() for i in range(1,21): d[i]=i**2 - for k in d.keys(): + for k in d.keys(): print k - + printDict() @@ -963,7 +977,7 @@ def printList(): for i in range(1,21): li.append(i**2) print li - + printList() @@ -986,7 +1000,7 @@ def printList(): for i in range(1,21): li.append(i**2) print li[:5] - + printList() @@ -1010,7 +1024,7 @@ def printList(): for i in range(1,21): li.append(i**2) print li[-5:] - + printList() @@ -1034,7 +1048,7 @@ def printList(): for i in range(1,21): li.append(i**2) print li[5:] - + printList() @@ -1043,7 +1057,7 @@ printList() 2.10 Question: -Define a function which can generate and print a tuple where the value are square of numbers between 1 and 20 (both included). +Define a function which can generate and print a tuple where the value are square of numbers between 1 and 20 (both included). Hints: @@ -1058,7 +1072,7 @@ def printTuple(): for i in range(1,21): li.append(i**2) print tuple(li) - + printTuple() @@ -1067,7 +1081,7 @@ printTuple() 2.10 Question: -With a given tuple (1,2,3,4,5,6,7,8,9,10), write a program to print the first half values in one line and the last half values in one line. +With a given tuple (1,2,3,4,5,6,7,8,9,10), write a program to print the first half values in one line and the last half values in one line. Hints: @@ -1085,7 +1099,7 @@ print tp2 2.10 Question: -Write a program to generate and print another tuple whose values are even numbers in the given tuple (1,2,3,4,5,6,7,8,9,10). +Write a program to generate and print another tuple whose values are even numbers in the given tuple (1,2,3,4,5,6,7,8,9,10). Hints: @@ -1108,7 +1122,7 @@ print tp2 2.14 Question: -Write a program which accepts a string as input to print "Yes" if the string is "yes" or "YES" or "Yes", otherwise print "No". +Write a program which accepts a string as input to print "Yes" if the string is "yes" or "YES" or "Yes", otherwise print "No". Hints: @@ -1238,7 +1252,7 @@ American.printNationality() 7.2 Question: -Define a class named American and its subclass NewYorker. +Define a class named American and its subclass NewYorker. Hints: @@ -1266,7 +1280,7 @@ print aNewYorker 7.2 Question: -Define a class named Circle which can be constructed by a radius. The Circle class has a method which can compute the area. +Define a class named Circle which can be constructed by a radius. The Circle class has a method which can compute the area. Hints: @@ -1293,7 +1307,7 @@ print aCircle.area() 7.2 -Define a class named Rectangle which can be constructed by a length and width. The Rectangle class has a method which can compute the area. +Define a class named Rectangle which can be constructed by a length and width. The Rectangle class has a method which can compute the area. Hints: @@ -2349,7 +2363,7 @@ print list(itertools.permutations([1,2,3])) #----------------------------------------# Question: -Write a program to solve a classic ancient Chinese puzzle: +Write a program to solve a classic ancient Chinese puzzle: We count 35 heads and 94 legs among the chickens and rabbits in a farm. How many rabbits and how many chickens do we have? Hint: From e5bb39b2ccced881884e98e3261dd62b32e1c44e Mon Sep 17 00:00:00 2001 From: Jakob Merz Date: Sun, 26 May 2019 21:58:56 +0200 Subject: [PATCH 02/23] Excercise 1 refactor --- ... Python challenging programming exercises.txt | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/100+ Python challenging programming exercises.txt b/100+ Python challenging programming exercises.txt index 22ee182c..26fd0c37 100644 --- a/100+ Python challenging programming exercises.txt +++ b/100+ Python challenging programming exercises.txt @@ -24,11 +24,14 @@ Write a program which will find all such numbers which are divisible by 7 but ar between 2000 and 3200 (both included). The numbers obtained should be printed in a comma-separated sequence on a single line. -x = range(2000,3200) -x.each( - ) +numbers = range(2000,3200) +result = [] +for number in numbers: + if (number % 7 == 0) and (number % 5 != 0): + result.append(str(number)) + +print(', '.join(result)) -print(x) Hints: Consider use range(#begin, #end) method @@ -36,11 +39,6 @@ Consider use range(#begin, #end) method - - - - - Solution: l=[] for i in range(2000, 3201): From 10a511049d5cb2440483f326fbf2075bb64d7304 Mon Sep 17 00:00:00 2001 From: Jakob Merz Date: Sun, 26 May 2019 22:11:07 +0200 Subject: [PATCH 03/23] Excercise 2 functional --- 100+ Python challenging programming exercises.txt | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/100+ Python challenging programming exercises.txt b/100+ Python challenging programming exercises.txt index 26fd0c37..fe57b972 100644 --- a/100+ Python challenging programming exercises.txt +++ b/100+ Python challenging programming exercises.txt @@ -63,6 +63,18 @@ Then, the output should be: Hints: In case of input data being supplied to the question, it should be assumed to be a console input. + +def factorializing(x): + y = x + while x > 1: + y = y * (x - 1) + x = x - 1 + + return y + +num1 = int(input()) +factorializing(num1) + Solution: def fact(x): if x == 0: From d1ed6f09900c6db2995348a3bf89e705a4ae639e Mon Sep 17 00:00:00 2001 From: Jakob Merz Date: Sun, 26 May 2019 22:14:55 +0200 Subject: [PATCH 04/23] Excercise 2 refactor --- ...ython challenging programming exercises.txt | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/100+ Python challenging programming exercises.txt b/100+ Python challenging programming exercises.txt index fe57b972..3a250f11 100644 --- a/100+ Python challenging programming exercises.txt +++ b/100+ Python challenging programming exercises.txt @@ -33,8 +33,8 @@ for number in numbers: print(', '.join(result)) -Hints: -Consider use range(#begin, #end) method +# Hints: +# Consider use range(#begin, #end) method @@ -64,16 +64,14 @@ Hints: In case of input data being supplied to the question, it should be assumed to be a console input. -def factorializing(x): - y = x - while x > 1: - y = y * (x - 1) - x = x - 1 +def fact(x): + if x == 0 + return 1 + return x * fact(x-1) - return y +x = int(input) +print(fact(x)) -num1 = int(input()) -factorializing(num1) Solution: def fact(x): From db40a6a2b729b28cdcf280d870854dfcc86d201a Mon Sep 17 00:00:00 2001 From: Jakob Merz Date: Mon, 27 May 2019 20:57:38 +0200 Subject: [PATCH 05/23] Excercise 3 functional --- ...thon challenging programming exercises.txt | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/100+ Python challenging programming exercises.txt b/100+ Python challenging programming exercises.txt index 3a250f11..7e102eea 100644 --- a/100+ Python challenging programming exercises.txt +++ b/100+ Python challenging programming exercises.txt @@ -98,6 +98,28 @@ Hints: In case of input data being supplied to the question, it should be assumed to be a console input. Consider use dict() +def multi(x): + count = 1 + #create empty dictionary + output = dict() + # While 1 < als x + while count <= x: + # create key value pair with 1: 1*1 + output[count] = count * count + # increase by 1 and check if smaller than x + count += 1 + return output + + +x = int(input) +print(multi(x)) + + + + + + + Solution: n=int(raw_input()) d=dict() From b5816d44457317e79e0313acf6367f42dcbeab26 Mon Sep 17 00:00:00 2001 From: Jakob Merz Date: Mon, 27 May 2019 21:02:02 +0200 Subject: [PATCH 06/23] formatting of solutions --- ...thon challenging programming exercises.txt | 85 ++++++++++--------- 1 file changed, 44 insertions(+), 41 deletions(-) diff --git a/100+ Python challenging programming exercises.txt b/100+ Python challenging programming exercises.txt index 7e102eea..0b35ea5a 100644 --- a/100+ Python challenging programming exercises.txt +++ b/100+ Python challenging programming exercises.txt @@ -1,17 +1,17 @@ 100+ Python challenging programming exercises 1. Level description -Level Description -Level 1 Beginner means someone who has just gone through an introductory Python course. He can solve some problems with 1 or 2 Python classes or functions. Normally, the answers could directly be found in the textbooks. -Level 2 Intermediate means someone who has just learned Python, but already has a relatively strong programming background from before. He should be able to solve problems which may involve 3 or 3 Python classes or functions. The answers cannot be directly be found in the textbooks. -Level 3 Advanced. He should use Python to solve more complex problem using more rich libraries functions and data structures and algorithms. He is supposed to solve the problem using several Python standard packages and advanced techniques. +# Level Description +# Level 1 Beginner means someone who has just gone through an introductory Python course. He can solve some problems with 1 or 2 Python classes or functions. Normally, the answers could directly be found in the textbooks. +# Level 2 Intermediate means someone who has just learned Python, but already has a relatively strong programming background from before. He should be able to solve problems which may involve 3 or 3 Python classes or functions. The answers cannot be directly be found in the textbooks. +# Level 3 Advanced. He should use Python to solve more complex problem using more rich libraries functions and data structures and algorithms. He is supposed to solve the problem using several Python standard packages and advanced techniques. 2. Problem template #----------------------------------------# -Question -Hints -Solution +# Question +# Hints +# Solution 3. Questions @@ -24,6 +24,10 @@ Write a program which will find all such numbers which are divisible by 7 but ar between 2000 and 3200 (both included). The numbers obtained should be printed in a comma-separated sequence on a single line. + +---- +Jakob Solution: + numbers = range(2000,3200) result = [] for number in numbers: @@ -31,21 +35,17 @@ for number in numbers: result.append(str(number)) print(', '.join(result)) - +---- # Hints: # Consider use range(#begin, #end) method +# Solution: +# l=[] +# for i in range(2000, 3201): +# if (i%7==0) and (i%5!=0): +# l.append(str(i)) - - - -Solution: -l=[] -for i in range(2000, 3201): - if (i%7==0) and (i%5!=0): - l.append(str(i)) - -print ','.join(l) +# print ','.join(l) #----------------------------------------# #----------------------------------------# @@ -63,6 +63,8 @@ Then, the output should be: Hints: In case of input data being supplied to the question, it should be assumed to be a console input. +---- +Jakob solution: def fact(x): if x == 0 @@ -71,16 +73,16 @@ def fact(x): x = int(input) print(fact(x)) +----- +# Solution: +# def fact(x): +# if x == 0: +# return 1 +# return x * fact(x - 1) -Solution: -def fact(x): - if x == 0: - return 1 - return x * fact(x - 1) - -x=int(raw_input()) -print fact(x) +# x=int(raw_input()) +# print fact(x) #----------------------------------------# #----------------------------------------# @@ -98,6 +100,9 @@ Hints: In case of input data being supplied to the question, it should be assumed to be a console input. Consider use dict() +---- +Jakob solution: + def multi(x): count = 1 #create empty dictionary @@ -110,23 +115,16 @@ def multi(x): count += 1 return output - -x = int(input) +x = int(raw_input) print(multi(x)) +----- +# Solution: +# n=int(raw_input()) +# d=dict() +# for i in range(1,n+1): +# d[i]=i*i - - - - - - -Solution: -n=int(raw_input()) -d=dict() -for i in range(1,n+1): - d[i]=i*i - -print d +# print d #----------------------------------------# #----------------------------------------# @@ -145,6 +143,11 @@ Hints: In case of input data being supplied to the question, it should be assumed to be a console input. tuple() method can convert list to tuple + + + + + Solution: values=raw_input() l=values.split(",") From 4dd756de39de2b539a24eb5301ab146f36871d19 Mon Sep 17 00:00:00 2001 From: Jakob Merz Date: Mon, 27 May 2019 21:15:09 +0200 Subject: [PATCH 07/23] Excercise 4 functional --- ...ython challenging programming exercises.txt | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/100+ Python challenging programming exercises.txt b/100+ Python challenging programming exercises.txt index 0b35ea5a..230f71aa 100644 --- a/100+ Python challenging programming exercises.txt +++ b/100+ Python challenging programming exercises.txt @@ -144,16 +144,20 @@ In case of input data being supplied to the question, it should be assumed to be tuple() method can convert list to tuple +input_value = input("Enter comma separated integers: ") +list_input = input_value.split(',') +tuple_input = tuple(list_input) +print(list_input) +print(tuple_input) - -Solution: -values=raw_input() -l=values.split(",") -t=tuple(l) -print l -print t +# Solution: +# values=raw_input() +# l=values.split(",") +# t=tuple(l) +# print l +# print t #----------------------------------------# #----------------------------------------# From aa564a0248221a4c8b0216ba8a38d950d940c9f1 Mon Sep 17 00:00:00 2001 From: Jakob Merz Date: Mon, 27 May 2019 21:24:22 +0200 Subject: [PATCH 08/23] Excercise 5 functional --- ...thon challenging programming exercises.txt | 28 +++++++++++++++---- 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/100+ Python challenging programming exercises.txt b/100+ Python challenging programming exercises.txt index 230f71aa..4795c43d 100644 --- a/100+ Python challenging programming exercises.txt +++ b/100+ Python challenging programming exercises.txt @@ -173,16 +173,32 @@ Also please include simple test function to test the class methods. Hints: Use __init__ method to construct some parameters +class Converter(object): + def __init__(self): + self.s = '' + + def getString(self): + self.s = input() + + def Capitalized(self): + result = self.s.upper() + print(result) + +strObj = Converter() +strObj.getString() +strObj.Capitalized() + + Solution: class InputOutString(object): - def __init__(self): - self.s = "" + def __init__(self): + self.s = "" - def getString(self): - self.s = raw_input() + def getString(self): + self.s = raw_input() - def printString(self): - print self.s.upper() + def printString(self): + print self.s.upper() strObj = InputOutString() strObj.getString() From 7c088add446aa71413be34b6dfcd35dff16eb89f Mon Sep 17 00:00:00 2001 From: Jakob Merz Date: Tue, 28 May 2019 20:12:11 +0200 Subject: [PATCH 09/23] Excercise 5 functional --- ...thon challenging programming exercises.txt | 34 +++++++++++++------ 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/100+ Python challenging programming exercises.txt b/100+ Python challenging programming exercises.txt index 4795c43d..6f16bdfb 100644 --- a/100+ Python challenging programming exercises.txt +++ b/100+ Python challenging programming exercises.txt @@ -189,20 +189,20 @@ strObj.getString() strObj.Capitalized() -Solution: -class InputOutString(object): - def __init__(self): - self.s = "" +# Solution: +# class InputOutString(object): +# def __init__(self): +# self.s = "" - def getString(self): - self.s = raw_input() +# def getString(self): +# self.s = raw_input() - def printString(self): - print self.s.upper() +# def printString(self): +# print self.s.upper() -strObj = InputOutString() -strObj.getString() -strObj.printString() +# strObj = InputOutString() +# strObj.getString() +# strObj.printString() #----------------------------------------# #----------------------------------------# @@ -221,6 +221,18 @@ Let us assume the following comma separated input sequence is given to the progr The output of the program should be: 18,22,24 +import math +D = [ int(x) for x in input().split(',') ] +C = 50 +H = 30 +result = [] +for i in D: + Q = round(math.sqrt((2*C*i)/H)) + result.append(Q) + +print(result) + + Hints: If the output received is in decimal form, it should be rounded off to its nearest value (for example, if the output received is 26.0, it should be printed as 26) In case of input data being supplied to the question, it should be assumed to be a console input. From db9f9832d759aac1bbf1426822bde03d0326c01d Mon Sep 17 00:00:00 2001 From: Jakob Merz Date: Tue, 28 May 2019 20:17:35 +0200 Subject: [PATCH 10/23] Excercise 6 refactor --- ...thon challenging programming exercises.txt | 42 ++++++++++--------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/100+ Python challenging programming exercises.txt b/100+ Python challenging programming exercises.txt index 6f16bdfb..34291569 100644 --- a/100+ Python challenging programming exercises.txt +++ b/100+ Python challenging programming exercises.txt @@ -221,33 +221,35 @@ Let us assume the following comma separated input sequence is given to the progr The output of the program should be: 18,22,24 +---- +Solution Jakob: +---- + import math -D = [ int(x) for x in input().split(',') ] +items = [ int(x) for x in input().split(',') ] C = 50 H = 30 result = [] -for i in D: - Q = round(math.sqrt((2*C*i)/H)) - result.append(Q) - -print(result) +for D in items: + result.append(str(round(math.sqrt((2*C*D)/H)))) +print(','.join(result)) -Hints: -If the output received is in decimal form, it should be rounded off to its nearest value (for example, if the output received is 26.0, it should be printed as 26) -In case of input data being supplied to the question, it should be assumed to be a console input. - -Solution: -#!/usr/bin/env python -import math -c=50 -h=30 -value = [] -items=[x for x in raw_input().split(',')] -for d in items: - value.append(str(int(round(math.sqrt(2*c*float(d)/h))))) +# Hints: +# If the output received is in decimal form, it should be rounded off to its nearest value (for example, if the output received is 26.0, it should be printed as 26) +# In case of input data being supplied to the question, it should be assumed to be a console input. -print ','.join(value) +# Solution: +# #!/usr/bin/env python +# import math +# c=50 +# h=30 +# value = [] +# items=[x for x in raw_input().split(',')] +# for d in items: +# value.append(str(int(round(math.sqrt(2*c*float(d)/h))))) + +# print ','.join(value) #----------------------------------------# #----------------------------------------# From ec287e3623a97222d13d72a1fe9c98a20a5fca20 Mon Sep 17 00:00:00 2001 From: Jakob Merz Date: Tue, 28 May 2019 23:07:19 +0200 Subject: [PATCH 11/23] Excercise 7 functional --- ...thon challenging programming exercises.txt | 40 +++++++++++++------ 1 file changed, 28 insertions(+), 12 deletions(-) diff --git a/100+ Python challenging programming exercises.txt b/100+ Python challenging programming exercises.txt index 34291569..55b89071 100644 --- a/100+ Python challenging programming exercises.txt +++ b/100+ Python challenging programming exercises.txt @@ -265,21 +265,37 @@ Suppose the following inputs are given to the program: Then, the output of the program should be: [[0, 0, 0, 0, 0], [0, 1, 2, 3, 4], [0, 2, 4, 6, 8]] -Hints: -Note: In case of input data being supplied to the question, it should be assumed to be a console input in a comma-separated form. +---- +Jakob solution: +---- -Solution: -input_str = raw_input() -dimensions=[int(x) for x in input_str.split(',')] -rowNum=dimensions[0] -colNum=dimensions[1] -multilist = [[0 for col in range(colNum)] for row in range(rowNum)] +digits = [int(x) for x in input().split(',')] +rows = digits[0] +cols = digits[1] +array = [[0 for i in range(cols)] for j in range(rows)] + +for row in range(rows): + for col in range(cols): + array[row][col] = row * col + +print(array) + + +# Hints: +# Note: In case of input data being supplied to the question, it should be assumed to be a console input in a comma-separated form. + +# Solution: +# input_str = raw_input() +# dimensions=[int(x) for x in input_str.split(',')] +# rowNum=dimensions[0] +# colNum=dimensions[1] +# multilist = [[0 for col in range(colNum)] for row in range(rowNum)] -for row in range(rowNum): - for col in range(colNum): - multilist[row][col]= row*col +# for row in range(rowNum): +# for col in range(colNum): +# multilist[row][col]= row*col -print multilist +# print multilist #----------------------------------------# #----------------------------------------# From a49eaefe01f80010893a7b0efd3bdde36abf0276 Mon Sep 17 00:00:00 2001 From: Jakob Merz Date: Wed, 29 May 2019 21:55:42 +0200 Subject: [PATCH 12/23] Excercise 8 & 9 functional --- ...thon challenging programming exercises.txt | 21 ++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/100+ Python challenging programming exercises.txt b/100+ Python challenging programming exercises.txt index 55b89071..d8e69f2c 100644 --- a/100+ Python challenging programming exercises.txt +++ b/100+ Python challenging programming exercises.txt @@ -102,6 +102,7 @@ Consider use dict() ---- Jakob solution: +----- def multi(x): count = 1 @@ -117,7 +118,6 @@ def multi(x): x = int(raw_input) print(multi(x)) ------ # Solution: # n=int(raw_input()) # d=dict() @@ -309,13 +309,20 @@ without,hello,bag,world Then, the output should be: bag,hello,without,world -Hints: -In case of input data being supplied to the question, it should be assumed to be a console input. +---- +Jakob solution: +---- + raw_words = [x for x in input().split(',')] + print(','.join(sorted(raw_words))) -Solution: -items=[x for x in raw_input().split(',')] -items.sort() -print ','.join(items) +# Hints: +# In case of input data being supplied to the question, it should be assumed to be a console input. + + +# Solution: +# items=[x for x in raw_input().split(',')] +# items.sort() +# print ','.join(items) #----------------------------------------# #----------------------------------------# From eb1a8a8174fc6caf5edc707ac32f46865ccff917 Mon Sep 17 00:00:00 2001 From: Jakob Merz Date: Wed, 29 May 2019 22:07:53 +0200 Subject: [PATCH 13/23] Excercise 9 functional --- ...thon challenging programming exercises.txt | 38 ++++++++++++++----- 1 file changed, 28 insertions(+), 10 deletions(-) diff --git a/100+ Python challenging programming exercises.txt b/100+ Python challenging programming exercises.txt index d8e69f2c..ecb1803d 100644 --- a/100+ Python challenging programming exercises.txt +++ b/100+ Python challenging programming exercises.txt @@ -338,20 +338,38 @@ Then, the output should be: HELLO WORLD PRACTICE MAKES PERFECT -Hints: -In case of input data being supplied to the question, it should be assumed to be a console input. - -Solution: +---- +Jakob Solution: +---- lines = [] while True: - s = raw_input() - if s: - lines.append(s.upper()) +# while line contains input + line = input() + if line: + # add to emty query and uppercase it + lines.append(line.upper()) else: - break; + # if empty line provided break loop + break +# join list by adding a line break between each element +text = '\n'.join(lines) + +for l in lines: + print(l) +# Hints: +# In case of input data being supplied to the question, it should be assumed to be a console input. -for sentence in lines: - print sentence +# Solution: +# lines = [] +# while True: +# s = raw_input() +# if s: +# lines.append(s.upper()) +# else: +# break; + +# for sentence in lines: +# print sentence #----------------------------------------# #----------------------------------------# From 9b18c8177a4513fd64949e68fc2cb3a1b861b15e Mon Sep 17 00:00:00 2001 From: Jakob Merz Date: Wed, 29 May 2019 22:17:19 +0200 Subject: [PATCH 14/23] Excercise 10 functional --- ...thon challenging programming exercises.txt | 22 +++++++++++++------ 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/100+ Python challenging programming exercises.txt b/100+ Python challenging programming exercises.txt index ecb1803d..1f531bda 100644 --- a/100+ Python challenging programming exercises.txt +++ b/100+ Python challenging programming exercises.txt @@ -383,14 +383,22 @@ hello world and practice makes perfect and hello world again Then, the output should be: again and hello makes perfect practice world -Hints: -In case of input data being supplied to the question, it should be assumed to be a console input. -We use set container to remove duplicated data automatically and then use sorted() to sort the data. +s = input() +words = [word for word in s.split(' ')] +# turn list into keys in dict (which can only occur once) +# turn dict into list +# sort list +# join with whitespace in between +print(' '.join(sorted(list(dict.fromkeys(words))))) -Solution: -s = raw_input() -words = [word for word in s.split(" ")] -print " ".join(sorted(list(set(words)))) +# Hints: +# In case of input data being supplied to the question, it should be assumed to be a console input. +# We use set container to remove duplicated data automatically and then use sorted() to sort the data. + +# Solution: +# s = raw_input() +# words = [word for word in s.split(" ")] +# print " ".join(sorted(list(set(words)))) #----------------------------------------# #----------------------------------------# From f29722b80e7c181ccf3661c2533f2bc01d999623 Mon Sep 17 00:00:00 2001 From: Jakob Merz Date: Tue, 4 Jun 2019 22:25:30 +0200 Subject: [PATCH 15/23] Excercise 12 functional --- ...thon challenging programming exercises.txt | 58 +++++++++++++------ 1 file changed, 39 insertions(+), 19 deletions(-) diff --git a/100+ Python challenging programming exercises.txt b/100+ Python challenging programming exercises.txt index 1f531bda..fc7bce1b 100644 --- a/100+ Python challenging programming exercises.txt +++ b/100+ Python challenging programming exercises.txt @@ -413,18 +413,26 @@ Then the output should be: 1010 Notes: Assume the data is input by console. -Hints: -In case of input data being supplied to the question, it should be assumed to be a console input. +output = [] +numbers = [x for x in input().split(',')] +for number in numbers: + if number[0] != '0': + if int(number) % 5 == 0: + output.append((number)) -Solution: -value = [] -items=[x for x in raw_input().split(',')] -for p in items: - intp = int(p, 2) - if not intp%5: - value.append(p) +print(','.join(output)) -print ','.join(value) +# Hints: +# In case of input data being supplied to the question, it should be assumed to be a console input. +# Solution: +# value = [] +# items=[x for x in raw_input().split(',')] +# for p in items: +# intp = int(p, 2) +# if not intp%5: +# value.append(p) + +# print ','.join(value) #----------------------------------------# #----------------------------------------# @@ -435,16 +443,28 @@ Question: Write a program, which will find all such numbers between 1000 and 3000 (both included) such that each digit of the number is an even number. The numbers obtained should be printed in a comma-separated sequence on a single line. -Hints: -In case of input data being supplied to the question, it should be assumed to be a console input. +---- +Jakob Solution: +---- +result = [] -Solution: -values = [] -for i in range(1000, 3001): - s = str(i) - if (int(s[0])%2==0) and (int(s[1])%2==0) and (int(s[2])%2==0) and (int(s[3])%2==0): - values.append(s) -print ",".join(values) + values = range(1000,3000) + for value in values: + if all(int(integer) % 2 == 0 for integer in str(value)): + result.append(value) +print(result) + + +# Hints: +# In case of input data being supplied to the question, it should be assumed to be a console input. + +# Solution: +# values = [] +# for i in range(1000, 3001): +# s = str(i) +# if (int(s[0])%2==0) and (int(s[1])%2==0) and (int(s[2])%2==0) and (int(s[3])%2==0): +# values.append(s) +# print ",".join(values) #----------------------------------------# #----------------------------------------# From 119ae27a849fc4bb49c967dec0d5c7c19affc1a5 Mon Sep 17 00:00:00 2001 From: Jakob Merz Date: Tue, 4 Jun 2019 22:34:07 +0200 Subject: [PATCH 16/23] Excercise 13 functional --- ...thon challenging programming exercises.txt | 37 ++++++++++++------- 1 file changed, 23 insertions(+), 14 deletions(-) diff --git a/100+ Python challenging programming exercises.txt b/100+ Python challenging programming exercises.txt index fc7bce1b..67d492e5 100644 --- a/100+ Python challenging programming exercises.txt +++ b/100+ Python challenging programming exercises.txt @@ -479,21 +479,30 @@ Then, the output should be: LETTERS 10 DIGITS 3 -Hints: -In case of input data being supplied to the question, it should be assumed to be a console input. +#split string into individual pieces -Solution: -s = raw_input() -d={"DIGITS":0, "LETTERS":0} -for c in s: - if c.isdigit(): - d["DIGITS"]+=1 - elif c.isalpha(): - d["LETTERS"]+=1 - else: - pass -print "LETTERS", d["LETTERS"] -print "DIGITS", d["DIGITS"] + +s = input() +numbers = sum(i.isdigit() for i in str(s)) +letters = sum(i.isalpha() for i in str(s)) +print('LETTERS:',letters) +print('NUMBERS:',numbers) + +# Hints: +# In case of input data being supplied to the question, it should be assumed to be a console input. + +# Solution: +# s = raw_input() +# d={"DIGITS":0, "LETTERS":0} +# for c in s: +# if c.isdigit(): +# d["DIGITS"]+=1 +# elif c.isalpha(): +# d["LETTERS"]+=1 +# else: +# pass +# print "LETTERS", d["LETTERS"] +# print "DIGITS", d["DIGITS"] #----------------------------------------# #----------------------------------------# From 5b4eb95b0bc71efa35332eecf841b6c927587197 Mon Sep 17 00:00:00 2001 From: Jakob Merz Date: Tue, 4 Jun 2019 22:50:31 +0200 Subject: [PATCH 17/23] Excercise 14 functional --- ...thon challenging programming exercises.txt | 41 ++++++++++++------- 1 file changed, 26 insertions(+), 15 deletions(-) diff --git a/100+ Python challenging programming exercises.txt b/100+ Python challenging programming exercises.txt index 67d492e5..5c356990 100644 --- a/100+ Python challenging programming exercises.txt +++ b/100+ Python challenging programming exercises.txt @@ -481,7 +481,9 @@ DIGITS 3 #split string into individual pieces - +---- +Jakob Solution: +---- s = input() numbers = sum(i.isdigit() for i in str(s)) letters = sum(i.isalpha() for i in str(s)) @@ -517,21 +519,30 @@ Then, the output should be: UPPER CASE 1 LOWER CASE 9 -Hints: -In case of input data being supplied to the question, it should be assumed to be a console input. +---- +Jakob Solution: +---- +s = input() +upper = sum(i.isupper() for i in str(s)) +lower = sum(i.islower() for i in str(s)) -Solution: -s = raw_input() -d={"UPPER CASE":0, "LOWER CASE":0} -for c in s: - if c.isupper(): - d["UPPER CASE"]+=1 - elif c.islower(): - d["LOWER CASE"]+=1 - else: - pass -print "UPPER CASE", d["UPPER CASE"] -print "LOWER CASE", d["LOWER CASE"] +print('UPPER CASE:',upper) +print('LOWER CASE:',lower) + +# Hints: +# In case of input data being supplied to the question, it should be assumed to be a console input. +# Solution: +# s = raw_input() +# d={"UPPER CASE":0, "LOWER CASE":0} +# for c in s: +# if c.isupper(): +# d["UPPER CASE"]+=1 +# elif c.islower(): +# d["LOWER CASE"]+=1 +# else: +# pass +# print "UPPER CASE", d["UPPER CASE"] +# print "LOWER CASE", d["LOWER CASE"] #----------------------------------------# #----------------------------------------# From 9955c650f0d7c19f51cd712c79fba0c335ce87c2 Mon Sep 17 00:00:00 2001 From: Jakob Merz Date: Tue, 4 Jun 2019 22:57:54 +0200 Subject: [PATCH 18/23] Excercise 15 functional --- ...thon challenging programming exercises.txt | 23 +++++++++++-------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/100+ Python challenging programming exercises.txt b/100+ Python challenging programming exercises.txt index 5c356990..8c5b2e2f 100644 --- a/100+ Python challenging programming exercises.txt +++ b/100+ Python challenging programming exercises.txt @@ -556,16 +556,21 @@ Suppose the following input is supplied to the program: Then, the output should be: 11106 -Hints: -In case of input data being supplied to the question, it should be assumed to be a console input. +d = input() +result = int(d) + int(d*2) + int(d*3) + int(d*4) +print(result) -Solution: -a = raw_input() -n1 = int( "%s" % a ) -n2 = int( "%s%s" % (a,a) ) -n3 = int( "%s%s%s" % (a,a,a) ) -n4 = int( "%s%s%s%s" % (a,a,a,a) ) -print n1+n2+n3+n4 + +# Hints: +# In case of input data being supplied to the question, it should be assumed to be a console input. + +# Solution: +# a = raw_input() +# n1 = int( "%s" % a ) +# n2 = int( "%s%s" % (a,a) ) +# n3 = int( "%s%s%s" % (a,a,a) ) +# n4 = int( "%s%s%s%s" % (a,a,a,a) ) +# print n1+n2+n3+n4 #----------------------------------------# #----------------------------------------# From b8c6a98a59de5819af6cfd53ff79c2c2ed658785 Mon Sep 17 00:00:00 2001 From: Jakob Merz Date: Tue, 4 Jun 2019 23:05:46 +0200 Subject: [PATCH 19/23] Excercise 16 functional --- 100+ Python challenging programming exercises.txt | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/100+ Python challenging programming exercises.txt b/100+ Python challenging programming exercises.txt index 8c5b2e2f..e449b6c2 100644 --- a/100+ Python challenging programming exercises.txt +++ b/100+ Python challenging programming exercises.txt @@ -584,6 +584,10 @@ Suppose the following input is supplied to the program: Then, the output should be: 1,3,5,7,9 + result = [i for i in input().split(',') if int(i) % 2 != 0] + print(','.join(result)) + + Hints: In case of input data being supplied to the question, it should be assumed to be a console input. From e4f2d93ab3b63c184f14d564ad735228a25a6020 Mon Sep 17 00:00:00 2001 From: Jakob Merz Date: Mon, 10 Jun 2019 11:20:59 +0200 Subject: [PATCH 20/23] Excercise 17 functional --- ...thon challenging programming exercises.txt | 40 ++++++++++++++----- 1 file changed, 30 insertions(+), 10 deletions(-) diff --git a/100+ Python challenging programming exercises.txt b/100+ Python challenging programming exercises.txt index e449b6c2..b22f9f2b 100644 --- a/100+ Python challenging programming exercises.txt +++ b/100+ Python challenging programming exercises.txt @@ -555,7 +555,9 @@ Suppose the following input is supplied to the program: 9 Then, the output should be: 11106 - +---- +Jakob Solution: +---- d = input() result = int(d) + int(d*2) + int(d*3) + int(d*4) print(result) @@ -584,18 +586,21 @@ Suppose the following input is supplied to the program: Then, the output should be: 1,3,5,7,9 - result = [i for i in input().split(',') if int(i) % 2 != 0] - print(','.join(result)) +---- +Jakob Solution: +---- +result = [i for i in input().split(',') if int(i) % 2 != 0] +print(','.join(result)) -Hints: -In case of input data being supplied to the question, it should be assumed to be a console input. +# Hints: +# In case of input data being supplied to the question, it should be assumed to be a console input. -Solution: -values = raw_input() -numbers = [x for x in values.split(",") if int(x)%2!=0] -print ",".join(numbers) -#----------------------------------------# +# Solution: +# values = raw_input() +# numbers = [x for x in values.split(",") if int(x)%2!=0] +# print ",".join(numbers) +# #----------------------------------------# Question 17 Level 2 @@ -614,6 +619,21 @@ D 100 Then, the output should be: 500 + +span = 2 +words = input().split(' ') +balance = 0 +transactions = [' '.join(words[i:i+span]) for i in range(0, len(words), span)] + +for transaction in transactions: + amount = [int(value) for value in transaction.split() if value.isdigit()] + if ('D') in transaction: + balance += amount[0] + elif ('W') in transaction: + balance -= amount[0] + +print(balance) + Hints: In case of input data being supplied to the question, it should be assumed to be a console input. From 754e079eba493e009df9c5b875a6fc7599c19755 Mon Sep 17 00:00:00 2001 From: Jakob Merz Date: Mon, 10 Jun 2019 12:09:38 +0200 Subject: [PATCH 21/23] Excercise 18 functional --- ...thon challenging programming exercises.txt | 97 +++++++++++-------- 1 file changed, 54 insertions(+), 43 deletions(-) diff --git a/100+ Python challenging programming exercises.txt b/100+ Python challenging programming exercises.txt index b22f9f2b..0d8dd7df 100644 --- a/100+ Python challenging programming exercises.txt +++ b/100+ Python challenging programming exercises.txt @@ -634,25 +634,25 @@ for transaction in transactions: print(balance) -Hints: -In case of input data being supplied to the question, it should be assumed to be a console input. +# Hints: +# In case of input data being supplied to the question, it should be assumed to be a console input. -Solution: -netAmount = 0 -while True: - s = raw_input() - if not s: - break - values = s.split(" ") - operation = values[0] - amount = int(values[1]) - if operation=="D": - netAmount+=amount - elif operation=="W": - netAmount-=amount - else: - pass -print netAmount +# Solution: +# netAmount = 0 +# while True: +# s = raw_input() +# if not s: +# break +# values = s.split(" ") +# operation = values[0] +# amount = int(values[1]) +# if operation=="D": +# netAmount+=amount +# elif operation=="W": +# netAmount-=amount +# else: +# pass +# print netAmount #----------------------------------------# #----------------------------------------# @@ -675,32 +675,43 @@ ABd1234@1,a F1#,2w3E*,2We3345 Then, the output of the program should be: ABd1234@1 -Hints: -In case of input data being supplied to the question, it should be assumed to be a console input. - -Solutions: import re -value = [] -items=[x for x in raw_input().split(',')] -for p in items: - if len(p)<6 or len(p)>12: - continue - else: - pass - if not re.search("[a-z]",p): - continue - elif not re.search("[0-9]",p): - continue - elif not re.search("[A-Z]",p): - continue - elif not re.search("[$#@]",p): - continue - elif re.search("\s",p): - continue - else: - pass - value.append(p) -print ",".join(value) +passwords = input().split(',') +result = [] +for password in passwords: + if len(password) >=6 and len(password)<= 12: + if re.match('[a-z]*', str(password)) and re.match('[A-Z]*', str(password)) and re.match('[0-9]*', str(password)): + if '#' in str(password) or '@' in str(password) or ('$' in str(password)): + result.append(password) + +print(''.join(result)) + +# Hints: +# In case of input data being supplied to the question, it should be assumed to be a console input. + +# Solutions: +# import re +# value = [] +# items=[x for x in raw_input().split(',')] +# for p in items: +# if len(p)<6 or len(p)>12: +# continue +# else: +# pass +# if not re.search("[a-z]",p): +# continue +# elif not re.search("[0-9]",p): +# continue +# elif not re.search("[A-Z]",p): +# continue +# elif not re.search("[$#@]",p): +# continue +# elif re.search("\s",p): +# continue +# else: +# pass +# value.append(p) +# print ",".join(value) #----------------------------------------# #----------------------------------------# From ad58d5731138de440063b8ae5fcfe36e75f7775a Mon Sep 17 00:00:00 2001 From: Jakob Merz Date: Sun, 16 Jun 2019 14:09:34 +0200 Subject: [PATCH 22/23] Excercise 19 functional --- .ipynb_checkpoints/Untitled-checkpoint.ipynb | 6 ++++++ 100+ Python challenging programming exercises.txt | 10 ++++++++++ Untitled.ipynb | 6 ++++++ 3 files changed, 22 insertions(+) create mode 100644 .ipynb_checkpoints/Untitled-checkpoint.ipynb create mode 100644 Untitled.ipynb diff --git a/.ipynb_checkpoints/Untitled-checkpoint.ipynb b/.ipynb_checkpoints/Untitled-checkpoint.ipynb new file mode 100644 index 00000000..2fd64429 --- /dev/null +++ b/.ipynb_checkpoints/Untitled-checkpoint.ipynb @@ -0,0 +1,6 @@ +{ + "cells": [], + "metadata": {}, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/100+ Python challenging programming exercises.txt b/100+ Python challenging programming exercises.txt index 0d8dd7df..bb7da76f 100644 --- a/100+ Python challenging programming exercises.txt +++ b/100+ Python challenging programming exercises.txt @@ -733,6 +733,16 @@ Json,21,85 Then, the output of the program should be: [('John', '20', '90'), ('Jony', '17', '91'), ('Jony', '17', '93'), ('Json', '21', '85'), ('Tom', '19', '80')] +from operator import itemgetter, attrgetter +people = [] +while True: + input_value = input() + if not input_value + break + people.append(tuple(input_value.split(','))) + +print(sorted(people, key=itemgetter(0,1,2))) + Hints: In case of input data being supplied to the question, it should be assumed to be a console input. We use itemgetter to enable multiple sort keys. diff --git a/Untitled.ipynb b/Untitled.ipynb new file mode 100644 index 00000000..2fd64429 --- /dev/null +++ b/Untitled.ipynb @@ -0,0 +1,6 @@ +{ + "cells": [], + "metadata": {}, + "nbformat": 4, + "nbformat_minor": 2 +} From eb7a39c6cef0d252002f595893a47e4b84d762fb Mon Sep 17 00:00:00 2001 From: Jakob Merz Date: Sun, 16 Jun 2019 14:19:11 +0200 Subject: [PATCH 23/23] formatting of code for pull request - exc. 1-19 updated to python3 syntx --- .ipynb_checkpoints/Untitled-checkpoint.ipynb | 6 - ...thon challenging programming exercises.txt | 366 ++++-------------- Untitled.ipynb | 6 - 3 files changed, 72 insertions(+), 306 deletions(-) delete mode 100644 .ipynb_checkpoints/Untitled-checkpoint.ipynb delete mode 100644 Untitled.ipynb diff --git a/.ipynb_checkpoints/Untitled-checkpoint.ipynb b/.ipynb_checkpoints/Untitled-checkpoint.ipynb deleted file mode 100644 index 2fd64429..00000000 --- a/.ipynb_checkpoints/Untitled-checkpoint.ipynb +++ /dev/null @@ -1,6 +0,0 @@ -{ - "cells": [], - "metadata": {}, - "nbformat": 4, - "nbformat_minor": 2 -} diff --git a/100+ Python challenging programming exercises.txt b/100+ Python challenging programming exercises.txt index bb7da76f..bfd108b7 100644 --- a/100+ Python challenging programming exercises.txt +++ b/100+ Python challenging programming exercises.txt @@ -25,9 +25,9 @@ between 2000 and 3200 (both included). The numbers obtained should be printed in a comma-separated sequence on a single line. ----- -Jakob Solution: - +Hints: +Consider use range(#begin, #end) method +Solution: numbers = range(2000,3200) result = [] for number in numbers: @@ -35,17 +35,6 @@ for number in numbers: result.append(str(number)) print(', '.join(result)) ----- - -# Hints: -# Consider use range(#begin, #end) method -# Solution: -# l=[] -# for i in range(2000, 3201): -# if (i%7==0) and (i%5!=0): -# l.append(str(i)) - -# print ','.join(l) #----------------------------------------# #----------------------------------------# @@ -63,9 +52,7 @@ Then, the output should be: Hints: In case of input data being supplied to the question, it should be assumed to be a console input. ----- -Jakob solution: - +Solution: def fact(x): if x == 0 return 1 @@ -73,16 +60,6 @@ def fact(x): x = int(input) print(fact(x)) ------ - -# Solution: -# def fact(x): -# if x == 0: -# return 1 -# return x * fact(x - 1) - -# x=int(raw_input()) -# print fact(x) #----------------------------------------# #----------------------------------------# @@ -100,10 +77,7 @@ Hints: In case of input data being supplied to the question, it should be assumed to be a console input. Consider use dict() ----- -Jakob solution: ------ - +Solution: def multi(x): count = 1 #create empty dictionary @@ -118,13 +92,6 @@ def multi(x): x = int(raw_input) print(multi(x)) -# Solution: -# n=int(raw_input()) -# d=dict() -# for i in range(1,n+1): -# d[i]=i*i - -# print d #----------------------------------------# #----------------------------------------# @@ -144,6 +111,9 @@ In case of input data being supplied to the question, it should be assumed to be tuple() method can convert list to tuple + + +Solution: input_value = input("Enter comma separated integers: ") list_input = input_value.split(',') tuple_input = tuple(list_input) @@ -151,13 +121,6 @@ tuple_input = tuple(list_input) print(list_input) print(tuple_input) - -# Solution: -# values=raw_input() -# l=values.split(",") -# t=tuple(l) -# print l -# print t #----------------------------------------# #----------------------------------------# @@ -173,6 +136,7 @@ Also please include simple test function to test the class methods. Hints: Use __init__ method to construct some parameters +Solution: class Converter(object): def __init__(self): self.s = '' @@ -187,22 +151,6 @@ class Converter(object): strObj = Converter() strObj.getString() strObj.Capitalized() - - -# Solution: -# class InputOutString(object): -# def __init__(self): -# self.s = "" - -# def getString(self): -# self.s = raw_input() - -# def printString(self): -# print self.s.upper() - -# strObj = InputOutString() -# strObj.getString() -# strObj.printString() #----------------------------------------# #----------------------------------------# @@ -221,10 +169,12 @@ Let us assume the following comma separated input sequence is given to the progr The output of the program should be: 18,22,24 ----- -Solution Jakob: ----- +Hints: +If the output received is in decimal form, it should be rounded off to its nearest value (for example, if the output received is 26.0, it should be printed as 26) +In case of input data being supplied to the question, it should be assumed to be a console input. + +Solution: import math items = [ int(x) for x in input().split(',') ] C = 50 @@ -234,22 +184,6 @@ for D in items: result.append(str(round(math.sqrt((2*C*D)/H)))) print(','.join(result)) - -# Hints: -# If the output received is in decimal form, it should be rounded off to its nearest value (for example, if the output received is 26.0, it should be printed as 26) -# In case of input data being supplied to the question, it should be assumed to be a console input. - -# Solution: -# #!/usr/bin/env python -# import math -# c=50 -# h=30 -# value = [] -# items=[x for x in raw_input().split(',')] -# for d in items: -# value.append(str(int(round(math.sqrt(2*c*float(d)/h))))) - -# print ','.join(value) #----------------------------------------# #----------------------------------------# @@ -265,9 +199,10 @@ Suppose the following inputs are given to the program: Then, the output of the program should be: [[0, 0, 0, 0, 0], [0, 1, 2, 3, 4], [0, 2, 4, 6, 8]] ----- -Jakob solution: ----- +Hints: +Note: In case of input data being supplied to the question, it should be assumed to be a console input in a comma-separated form. + +Solution: digits = [int(x) for x in input().split(',')] rows = digits[0] @@ -279,23 +214,6 @@ for row in range(rows): array[row][col] = row * col print(array) - - -# Hints: -# Note: In case of input data being supplied to the question, it should be assumed to be a console input in a comma-separated form. - -# Solution: -# input_str = raw_input() -# dimensions=[int(x) for x in input_str.split(',')] -# rowNum=dimensions[0] -# colNum=dimensions[1] -# multilist = [[0 for col in range(colNum)] for row in range(rowNum)] - -# for row in range(rowNum): -# for col in range(colNum): -# multilist[row][col]= row*col - -# print multilist #----------------------------------------# #----------------------------------------# @@ -309,20 +227,14 @@ without,hello,bag,world Then, the output should be: bag,hello,without,world ----- -Jakob solution: ----- - raw_words = [x for x in input().split(',')] - print(','.join(sorted(raw_words))) +Hints: +In case of input data being supplied to the question, it should be assumed to be a console input. -# Hints: -# In case of input data being supplied to the question, it should be assumed to be a console input. +Solution: + raw_words = [x for x in input().split(',')] + print(','.join(sorted(raw_words))) -# Solution: -# items=[x for x in raw_input().split(',')] -# items.sort() -# print ','.join(items) #----------------------------------------# #----------------------------------------# @@ -338,9 +250,10 @@ Then, the output should be: HELLO WORLD PRACTICE MAKES PERFECT ----- -Jakob Solution: ----- +Hints: +In case of input data being supplied to the question, it should be assumed to be a console input. + +Solution: lines = [] while True: # while line contains input @@ -356,20 +269,6 @@ text = '\n'.join(lines) for l in lines: print(l) -# Hints: -# In case of input data being supplied to the question, it should be assumed to be a console input. - -# Solution: -# lines = [] -# while True: -# s = raw_input() -# if s: -# lines.append(s.upper()) -# else: -# break; - -# for sentence in lines: -# print sentence #----------------------------------------# #----------------------------------------# @@ -383,22 +282,16 @@ hello world and practice makes perfect and hello world again Then, the output should be: again and hello makes perfect practice world + + +Hints: +In case of input data being supplied to the question, it should be assumed to be a console input. +We use set container to remove duplicated data automatically and then use sorted() to sort the data. + +Solution: s = input() words = [word for word in s.split(' ')] -# turn list into keys in dict (which can only occur once) -# turn dict into list -# sort list -# join with whitespace in between print(' '.join(sorted(list(dict.fromkeys(words))))) - -# Hints: -# In case of input data being supplied to the question, it should be assumed to be a console input. -# We use set container to remove duplicated data automatically and then use sorted() to sort the data. - -# Solution: -# s = raw_input() -# words = [word for word in s.split(" ")] -# print " ".join(sorted(list(set(words)))) #----------------------------------------# #----------------------------------------# @@ -413,6 +306,9 @@ Then the output should be: 1010 Notes: Assume the data is input by console. +Hints: +In case of input data being supplied to the question, it should be assumed to be a console input. +Solution: output = [] numbers = [x for x in input().split(',')] for number in numbers: @@ -421,18 +317,6 @@ for number in numbers: output.append((number)) print(','.join(output)) - -# Hints: -# In case of input data being supplied to the question, it should be assumed to be a console input. -# Solution: -# value = [] -# items=[x for x in raw_input().split(',')] -# for p in items: -# intp = int(p, 2) -# if not intp%5: -# value.append(p) - -# print ','.join(value) #----------------------------------------# #----------------------------------------# @@ -443,9 +327,10 @@ Question: Write a program, which will find all such numbers between 1000 and 3000 (both included) such that each digit of the number is an even number. The numbers obtained should be printed in a comma-separated sequence on a single line. ----- -Jakob Solution: ----- +Hints: +In case of input data being supplied to the question, it should be assumed to be a console input. + +Solution: result = [] values = range(1000,3000) @@ -453,18 +338,6 @@ result = [] if all(int(integer) % 2 == 0 for integer in str(value)): result.append(value) print(result) - - -# Hints: -# In case of input data being supplied to the question, it should be assumed to be a console input. - -# Solution: -# values = [] -# for i in range(1000, 3001): -# s = str(i) -# if (int(s[0])%2==0) and (int(s[1])%2==0) and (int(s[2])%2==0) and (int(s[3])%2==0): -# values.append(s) -# print ",".join(values) #----------------------------------------# #----------------------------------------# @@ -481,30 +354,15 @@ DIGITS 3 #split string into individual pieces ----- -Jakob Solution: ----- +Hints: +In case of input data being supplied to the question, it should be assumed to be a console input. + +Solution: s = input() numbers = sum(i.isdigit() for i in str(s)) letters = sum(i.isalpha() for i in str(s)) print('LETTERS:',letters) print('NUMBERS:',numbers) - -# Hints: -# In case of input data being supplied to the question, it should be assumed to be a console input. - -# Solution: -# s = raw_input() -# d={"DIGITS":0, "LETTERS":0} -# for c in s: -# if c.isdigit(): -# d["DIGITS"]+=1 -# elif c.isalpha(): -# d["LETTERS"]+=1 -# else: -# pass -# print "LETTERS", d["LETTERS"] -# print "DIGITS", d["DIGITS"] #----------------------------------------# #----------------------------------------# @@ -519,30 +377,15 @@ Then, the output should be: UPPER CASE 1 LOWER CASE 9 ----- -Jakob Solution: ----- +Hints: +In case of input data being supplied to the question, it should be assumed to be a console input. +Solution: s = input() upper = sum(i.isupper() for i in str(s)) lower = sum(i.islower() for i in str(s)) print('UPPER CASE:',upper) print('LOWER CASE:',lower) - -# Hints: -# In case of input data being supplied to the question, it should be assumed to be a console input. -# Solution: -# s = raw_input() -# d={"UPPER CASE":0, "LOWER CASE":0} -# for c in s: -# if c.isupper(): -# d["UPPER CASE"]+=1 -# elif c.islower(): -# d["LOWER CASE"]+=1 -# else: -# pass -# print "UPPER CASE", d["UPPER CASE"] -# print "LOWER CASE", d["LOWER CASE"] #----------------------------------------# #----------------------------------------# @@ -555,24 +398,14 @@ Suppose the following input is supplied to the program: 9 Then, the output should be: 11106 ----- -Jakob Solution: ----- + +Hints: +In case of input data being supplied to the question, it should be assumed to be a console input. + +Solution: d = input() result = int(d) + int(d*2) + int(d*3) + int(d*4) print(result) - - -# Hints: -# In case of input data being supplied to the question, it should be assumed to be a console input. - -# Solution: -# a = raw_input() -# n1 = int( "%s" % a ) -# n2 = int( "%s%s" % (a,a) ) -# n3 = int( "%s%s%s" % (a,a,a) ) -# n4 = int( "%s%s%s%s" % (a,a,a,a) ) -# print n1+n2+n3+n4 #----------------------------------------# #----------------------------------------# @@ -586,20 +419,12 @@ Suppose the following input is supplied to the program: Then, the output should be: 1,3,5,7,9 ----- -Jakob Solution: ----- +Hints: +In case of input data being supplied to the question, it should be assumed to be a console input. + +Solution: result = [i for i in input().split(',') if int(i) % 2 != 0] print(','.join(result)) - - -# Hints: -# In case of input data being supplied to the question, it should be assumed to be a console input. - -# Solution: -# values = raw_input() -# numbers = [x for x in values.split(",") if int(x)%2!=0] -# print ",".join(numbers) # #----------------------------------------# Question 17 @@ -620,6 +445,11 @@ Then, the output should be: 500 +Hints: +In case of input data being supplied to the question, it should be assumed to be a console input. + +Solution: + span = 2 words = input().split(' ') balance = 0 @@ -633,26 +463,6 @@ for transaction in transactions: balance -= amount[0] print(balance) - -# Hints: -# In case of input data being supplied to the question, it should be assumed to be a console input. - -# Solution: -# netAmount = 0 -# while True: -# s = raw_input() -# if not s: -# break -# values = s.split(" ") -# operation = values[0] -# amount = int(values[1]) -# if operation=="D": -# netAmount+=amount -# elif operation=="W": -# netAmount-=amount -# else: -# pass -# print netAmount #----------------------------------------# #----------------------------------------# @@ -675,6 +485,10 @@ ABd1234@1,a F1#,2w3E*,2We3345 Then, the output of the program should be: ABd1234@1 +Hints: +In case of input data being supplied to the question, it should be assumed to be a console input. + +Solutions: import re passwords = input().split(',') result = [] @@ -685,33 +499,6 @@ for password in passwords: result.append(password) print(''.join(result)) - -# Hints: -# In case of input data being supplied to the question, it should be assumed to be a console input. - -# Solutions: -# import re -# value = [] -# items=[x for x in raw_input().split(',')] -# for p in items: -# if len(p)<6 or len(p)>12: -# continue -# else: -# pass -# if not re.search("[a-z]",p): -# continue -# elif not re.search("[0-9]",p): -# continue -# elif not re.search("[A-Z]",p): -# continue -# elif not re.search("[$#@]",p): -# continue -# elif re.search("\s",p): -# continue -# else: -# pass -# value.append(p) -# print ",".join(value) #----------------------------------------# #----------------------------------------# @@ -733,31 +520,22 @@ Json,21,85 Then, the output of the program should be: [('John', '20', '90'), ('Jony', '17', '91'), ('Jony', '17', '93'), ('Json', '21', '85'), ('Tom', '19', '80')] -from operator import itemgetter, attrgetter -people = [] -while True: - input_value = input() - if not input_value - break - people.append(tuple(input_value.split(','))) - -print(sorted(people, key=itemgetter(0,1,2))) Hints: In case of input data being supplied to the question, it should be assumed to be a console input. We use itemgetter to enable multiple sort keys. Solutions: -from operator import itemgetter, attrgetter -l = [] +from operator import itemgetter, attrgetter +people = [] while True: - s = raw_input() - if not s: + input_value = input() + if not input_value break - l.append(tuple(s.split(","))) + people.append(tuple(input_value.split(','))) -print sorted(l, key=itemgetter(0,1,2)) +print(sorted(people, key=itemgetter(0,1,2))) #----------------------------------------# #----------------------------------------# diff --git a/Untitled.ipynb b/Untitled.ipynb deleted file mode 100644 index 2fd64429..00000000 --- a/Untitled.ipynb +++ /dev/null @@ -1,6 +0,0 @@ -{ - "cells": [], - "metadata": {}, - "nbformat": 4, - "nbformat_minor": 2 -}