-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathchallenges.json
More file actions
127 lines (127 loc) · 16.6 KB
/
challenges.json
File metadata and controls
127 lines (127 loc) · 16.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
[
{
"challenge":"Write a program which will find all such numbers which are divisible by 7 but are not a multiple of 5,between 2000 and 3200 (both included).The numbers obtained should be printed in a comma-separated sequence on a single line.",
"id":1,
"level":"easy"
},
{
"challenge":"Write a program which can compute the factorial of a given numbers.The results should be printed in a comma-separated sequence on a single line.Suppose the following input is supplied to the program:8Then, the output should be:40320Hints:In case of input data being supplied to the question, it should be assumed to be a console input.Solution:def fact(x): if x == 0: return 1 return x * fact(x - 1)x=int(raw_input())print fact(x)",
"id":2,
"level":"easy"
},
{
"challenge":"With a given integral number n, write a program to generate a dictionary that contains (i, i*i) such that is an integral number between 1 and n (both included). and then the program should print the dictionary.Suppose the following input is supplied to the program:8Then, the output should be:{1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64}Hints:In case of input data being supplied to the question, it should be assumed to be a console input.Consider use dict()Solution:n=int(raw_input())d=dict()for i in range(1,n+1): d[i]=i*iprint d",
"id":3,
"level":"easy"
},
{
"challenge":"Write a program which accepts a sequence of comma-separated numbers from console and generate a list and a tuple which contains every number.Suppose the following input is supplied to the program:34,67,55,33,12,98Then, the output should be:['34', '67', '55', '33', '12', '98']('34', '67', '55', '33', '12', '98')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 tupleSolution:values=raw_input()l=values.split(\",\")t=tuple(l)print lprint t",
"id":4,
"level":"easy"
},
{
"challenge":"Define a class which has at least two methods:getString: to get a string from console inputprintString: to print the string in upper case.Also please include simple test function to test the class methods.Hints:Use __init__ method to construct some parametersSolution: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()",
"id":5,
"level":"easy"
},
{
"challenge":"Write a program that calculates and prints the value according to the given formula:Q = Square root of [(2 * C * D)/H]Following are the fixed values of C and H:C is 50. H is 30.D is the variable whose values should be input to your program in a comma-separated sequence.ExampleLet us assume the following comma separated input sequence is given to the program:100,150,180The output of the program should be:18,22,24Hints: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 pythonimport mathc=50h=30value = []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)",
"id":6,
"level":"medium"
},
{
"challenge":"Write a program which takes 2 digits, X,Y as input and generates a 2-dimensional array. The element value in the i-th row and j-th column of the array should be i*j.Note: i=0,1.., X-1; j=0,1,\u00a1\u00adY-1.ExampleSuppose the following inputs are given to the program:3,5Then, 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.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*colprint multilist",
"id":7,
"level":"medium"
},
{
"challenge":"Write a program that accepts a comma separated sequence of words as input and prints the words in a comma-separated sequence after sorting them alphabetically.Suppose the following input is supplied to the program:without,hello,bag,worldThen, the output should be:bag,hello,without,worldHints: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)",
"id":8,
"level":"medium"
},
{
"challenge":"Write a program that accepts sequence of lines as input and prints the lines after making all characters in the sentence capitalized.Suppose the following input is supplied to the program:Hello worldPractice makes perfectThen, the output should be:HELLO WORLDPRACTICE MAKES PERFECTHints: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",
"id":9,
"level":"medium"
},
{
"challenge":"Write a program that accepts a sequence of whitespace separated words as input and prints the words after removing all duplicate words and sorting them alphanumerically.Suppose the following input is supplied to the program:hello world and practice makes perfect and hello world againThen, the output should be:again and hello makes perfect practice worldHints: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))))",
"id":10,
"level":"medium"
},
{
"challenge":"Write a program which accepts a sequence of comma separated 4 digit binary numbers as its input and then check whether they are divisible by 5 or not. The numbers that are divisible by 5 are to be printed in a comma separated sequence.Example:0100,0011,1010,1001Then the output should be:1010Notes: 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: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)",
"id":11,
"level":"medium"
},
{
"challenge":"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.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)",
"id":12,
"level":"medium"
},
{
"challenge":"Write a program that accepts a sentence and calculate the number of letters and digits.Suppose the following input is supplied to the program:hello world! 123Then, the output should be:LETTERS 10DIGITS 3Hints: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: passprint \"LETTERS\", d[\"LETTERS\"]print \"DIGITS\", d[\"DIGITS\"]",
"id":13,
"level":"medium"
},
{
"challenge":"Write a program that accepts a sentence and calculate the number of upper case letters and lower case letters.Suppose the following input is supplied to the program:Hello world!Then, the output should be:UPPER CASE 1LOWER CASE 9Hints: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: passprint \"UPPER CASE\", d[\"UPPER CASE\"]print \"LOWER CASE\", d[\"LOWER CASE\"]",
"id":14,
"level":"medium"
},
{
"challenge":"Write a program that computes the value of a+aa+aaa+aaaa with a given digit as the value of a.Suppose the following input is supplied to the program:9Then, the output should be:11106Hints: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",
"id":15,
"level":"medium"
},
{
"challenge":"Use a list comprehension to square each odd number in a list. The list is input by a sequence of comma-separated numbers.Suppose the following input is supplied to the program:1,2,3,4,5,6,7,8,9Then, the output should be:1,3,5,7,9Hints: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)",
"id":16,
"level":"medium"
},
{
"challenge":"Write a program that computes the net amount of a bank account based a transaction log from console input. The transaction log format is shown as following:D 100W 200D means deposit while W means withdrawal.Suppose the following input is supplied to the program:D 300D 300W 200D 100Then, the output should be:500Hints:In case of input data being supplied to the question, it should be assumed to be a console input.Solution:netAmount = 0while 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: passprint netAmount",
"id":17,
"level":"medium"
},
{
"challenge":"A website requires the users to input username and password to register. Write a program to check the validity of password input by users.Following are the criteria for checking the password:1. At least 1 letter between [a-z]2. At least 1 number between [0-9]1. At least 1 letter between [A-Z]3. At least 1 character from [$#@]4. Minimum length of transaction password: 65. Maximum length of transaction password: 12Your program should accept a sequence of comma separated passwords and will check them according to the above criteria. Passwords that match the criteria are to be printed, each separated by a comma.ExampleIf the following passwords are given as input to the program:ABd1234@1,a F1#,2w3E*,2We3345Then, the output of the program should be:ABd1234@1Hints:In case of input data being supplied to the question, it should be assumed to be a console input.Solutions:import revalue = []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)",
"id":18,
"level":"hard"
},
{
"challenge":"You are required to write a program to sort the (name, age, height) tuples by ascending order where name is string, age and height are numbers. The tuples are input by console. The sort criteria is:1: Sort based on name;2: Then sort based on age;3: Then sort by score.The priority is that name > age > score.If the following tuples are given as input to the program:Tom,19,80John,20,90Jony,17,91Jony,17,93Json,21,85Then, the output of the program should be:[('John', '20', '90'), ('Jony', '17', '91'), ('Jony', '17', '93'), ('Json', '21', '85'), ('Tom', '19', '80')]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, attrgetterl = []while True: s = raw_input() if not s: break l.append(tuple(s.split(\",\")))print sorted(l, key=itemgetter(0,1,2))",
"id":19,
"level":"hard"
},
{
"challenge":"Define a class with a generator which can iterate the numbers, which are divisible by 7, between a given range 0 and n.Hints:Consider use yieldSolution:def putNumbers(n): i = 0 while i<n: j=i i=i+1 if j%7==0: yield jfor i in reverse(100): print i",
"id":20,
"level":"hard"
},
{
"challenge":"A robot moves in a plane starting from the original point (0,0). The robot can move toward UP, DOWN, LEFT and RIGHT with a given steps. The trace of robot movement is shown as the following:UP 5DOWN 3LEFT 3RIGHT 2\u00a1\u00adThe numbers after the direction are steps. Please write a program to compute the distance from current position after a sequence of movement and original point. If the distance is a float, then just print the nearest integer.Example:If the following tuples are given as input to the program:UP 5DOWN 3LEFT 3RIGHT 2Then, the output of the program should be:2Hints:In case of input data being supplied to the question, it should be assumed to be a console input.Solution:import mathpos = [0,0]while True: s = raw_input() if not s: break movement = s.split(\" \") direction = movement[0] steps = int(movement[1]) if direction==\"UP\": pos[0]+=steps elif direction==\"DOWN\": pos[0]-=steps elif direction==\"LEFT\": pos[1]-=steps elif direction==\"RIGHT\": pos[1]+=steps else: passprint int(round(math.sqrt(pos[1]**2+pos[0]**2)))",
"id":21,
"level":"hard"
},
{
"challenge":"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:2:23.:13?:1New:1Python:5Read:1and:1between:1choosing:1or:2to:1HintsIn case of input data being supplied to the question, it should be assumed to be a console input.Solution:freq = {} # frequency of words in textline = raw_input()for word in line.split(): freq[word] = freq.get(word,0)+1words = freq.keys()words.sort()for w in words: print \"%s:%d\" % (w,freq[w])",
"id":22,
"level":"hard"
},
{
"challenge":" Write a method which can calculate square value of numberHints: Using the ** operatorSolution:def square(num): return num ** 2print square(2)print square(3)",
"id":23,
"level":"level 1"
},
{
"challenge":" 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__Solution:print abs.__doc__print int.__doc__print raw_input.__doc__def square(num): '''Return the square value of the input number. The input number must be integer. ''' return num ** 2print square(2)print square.__doc__",
"id":24,
"level":"easy"
},
{
"challenge":" Define a class, which have a class parameter and have a same instance parameter.Hints: Define a instance parameter, need add it in __init__ method You can init a object with construct parameter or set the value laterSolution:class Person: # Define the class parameter \"name\" name = \"Person\" def __init__(self, name = None): # self.name is the instance parameter self.name = namejeffrey = Person(\"Jeffrey\")print \"%s name is %s\" % (Person.name, jeffrey.name)nico = Person()nico.name = \"Nico\"print \"%s name is %s\" % (Person.name, nico.name)",
"id":25,
"level":"easy"
}
]