Skip to content

Commit e28e6cd

Browse files
committed
python3 solution
1 parent e64d20a commit e28e6cd

File tree

7 files changed

+313
-0
lines changed

7 files changed

+313
-0
lines changed

py3_solution/exercises_2.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
'''
5+
Ask the user for a number.
6+
Depending on whether the number is even or odd, print out an appropriate message to the user.
7+
Hint: how does an even / odd number react differently when divided by 2?
8+
9+
Extras:
10+
11+
If the number is a multiple of 4, print out a different message.
12+
Ask the user for two numbers: one number to check (call it num) and one number to divide by (check).
13+
If check divides evenly into num, tell that to the user. If not, print a different appropriate message.
14+
'''
15+
16+
n = input('Please input a number: ')
17+
18+
n = int(n)
19+
20+
if n % 2 == 0:
21+
print('The number you input is even')
22+
23+
else:
24+
print('The number you input is odd')
25+
26+
if n % 4 == 0:
27+
print('and the number is a multiple of 4')
28+
29+
print('Check if a number could be divided evenly by another one: ')
30+
31+
num = input('Please input a number to check: ')
32+
divider = input('Please input a divider number: ')
33+
34+
num = int(num)
35+
divider = int(divider)
36+
37+
remainder = num % divider
38+
39+
if remainder == 0:
40+
print('{0} could be divided by {1} evenly'.format(num, divider))
41+
else:
42+
print('{0} could not be divided by {1} evenly, the remainder is {2}'.format(num, divider, remainder))

py3_solution/exercises_3.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
'''
5+
Take a list, say for example this one:
6+
7+
a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
8+
and write a program that prints out all the elements of the list that are less than 5.
9+
10+
Extras:
11+
12+
- Instead of printing the elements one by one,
13+
make a new list that has all the elements less than 5 from this list in it and print out this new list.
14+
15+
- Write this in one line of Python.
16+
17+
- Ask the user for a number and return a list that contains only elements from the original list that are smaller than the number given by the user.
18+
'''
19+
20+
numbers = input('Please input a group of numbers seperated by ",": ')
21+
number_list = [int(number.strip()) for number in numbers.split(',')]
22+
23+
print('Those are numbers in the group smaller than 5: ')
24+
for number in number_list:
25+
if number < 5:
26+
print(number)
27+
28+
##########################
29+
# For Extras:
30+
##########################
31+
32+
print('Filter numbers smaller than 5 to a new list: ')
33+
# Method 1:
34+
new_number_list = [number for number in number_list if number<5]
35+
print('filter list in Method 1: ', list(new_number_list))
36+
37+
# Method 2:
38+
new_number_list2 = filter(lambda x:x<5, number_list)
39+
print('filter list in Method 2: ', list(new_number_list2))
40+
41+
ceiling = input('Please input a custom filter number: ')
42+
ceiling = int(ceiling)
43+
44+
new_number_list3 = filter(lambda x:x<ceiling, number_list)
45+
print('The following are numbers that are smaller than {0}: '.format(ceiling), list(new_number_list3))

py3_solution/exercises_4.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
'''
5+
Create a program that asks the user for a number and then prints out a list of all the divisors of that number.
6+
7+
(If you don’t know what a divisor is, it is a number that divides evenly into another number.
8+
For example, 13 is a divisor of 26 because 26 / 13 has no remainder.)
9+
'''
10+
11+
n = input('Please input a number: ')
12+
n = int(n)
13+
14+
# The simplest solution
15+
res = []
16+
for i in range(2, n):
17+
if n % i == 0:
18+
res.append(i)
19+
20+
res.append(1)
21+
res.append(n)
22+
23+
res.sort()
24+
25+
print('The following are divisors of your input number: ')
26+
print(res)
27+
28+
# A more efficient solution
29+
30+
from math import sqrt
31+
32+
x = int(sqrt(n))
33+
34+
res2 = []
35+
for i in range(2, x):
36+
if n % i == 0:
37+
res2.append(i)
38+
res2.append(n//i)
39+
40+
if n == x * x:
41+
res.append(x)
42+
43+
res2.append(1)
44+
res2.append(n)
45+
46+
res2.sort()
47+
48+
print('The following are divisors of your input number calculated by another way: ')
49+
print(res2)

py3_solution/exercises_5.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
'''
5+
Take two lists, say for example these two:
6+
7+
a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
8+
b = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
9+
and write a program that returns a list that contains only the elements that are common between the lists (without duplicates). Make sure your program works on two lists of different sizes.
10+
11+
Extras:
12+
13+
- Randomly generate two lists to test this
14+
- Write this in one line of Python (don’t worry if you can’t figure this out at this point - we’ll get to it soon)
15+
'''
16+
17+
import random
18+
19+
a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
20+
b = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
21+
22+
######################################
23+
# Randomly generate lists a and b
24+
######################################
25+
length1 = random.randint(10,20)
26+
a = [random.randint(1,50) for i in range(length1)]
27+
28+
length2 = random.randint(10,20)
29+
b = [random.randint(1,50) for i in range(length2)]
30+
31+
print('list a: ', a)
32+
print('list b: ', b)
33+
34+
#######################################
35+
# Calculate intersection of a and b
36+
#######################################
37+
38+
# method 1:
39+
res = []
40+
for i in a:
41+
if i in b:
42+
res.append(i)
43+
44+
# Remove duplacates
45+
res = list(set(res))
46+
47+
print('Method 1:')
48+
print('The intersection of a&b is: ', res)
49+
50+
# method 2:
51+
res = filter(lambda x:x in b, a)
52+
res = list(set(res))
53+
print('Method 2:')
54+
print('The intersection of a&b is: ', res)
55+
56+
# method 3:
57+
res = list(set(a) & set(b))
58+
print('Method 3:')
59+
print('The intersection of a&b is: ', res)
60+

py3_solution/exercises_6.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
'''
5+
Ask the user for a string and print out whether this string is a palindrome or not.
6+
(A palindrome is a string that reads the same forwards and backwards.)
7+
'''
8+
9+
word = input('Please input a string for check: ')
10+
11+
# Method 1:
12+
reverse_word = word[::-1]
13+
14+
if word == reverse_word:
15+
print('The word you input is a palindrome')
16+
else:
17+
print('The word you input is not a palindrome')
18+
19+
# Method 2:
20+
is_palindrome = True
21+
length = len(word)
22+
for i in range(0, length//2):
23+
if word[i] != word[length-1-i]:
24+
is_palindrome = False
25+
break
26+
27+
if is_palindrome:
28+
print('The word you input is a palindrome')
29+
else:
30+
print('The word you input is not a palindrome')

py3_solution/exercises_7.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
'''
5+
Let’s say I give you a list saved in a variable: a = [1, 4, 9, 16, 25, 36, 49, 64, 81, 100].
6+
Write one line of Python that takes this list a and makes a new list that has only the even elements of this list in it.
7+
'''
8+
9+
a = [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
10+
print('The target list is: ', a)
11+
12+
b = [x for x in a if x%2==1]
13+
print('The new list that has only the even elements of previous list is: ')
14+
print(b)
15+
16+
c = filter(lambda x:x%2!=0, a)
17+
print('The new list that has only the even elements of previous list is: ')
18+
print(list(c))

py3_solution/exercises_8.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
'''
5+
Make a two-player Rock-Paper-Scissors game.
6+
(Hint: Ask for player plays (using input), compare them, print out a message of congratulations to the winner, and ask if the players want to start a new game)
7+
8+
9+
Remember the rules:
10+
11+
Rock beats scissors
12+
Scissors beats paper
13+
Paper beats rock
14+
'''
15+
16+
17+
rule = {
18+
'rock': 'paper',
19+
'paper': 'scissors',
20+
'scissors': 'rock'
21+
}
22+
23+
results = {
24+
0: 'Same input',
25+
1: 'playerA wins!',
26+
2: 'playerB wins!'
27+
}
28+
29+
def validate_input(val):
30+
if val in rule.keys():
31+
return True
32+
return False
33+
34+
def valid_input_factory(player):
35+
val = input('Input for {0}: '.format(player)).lower()
36+
while not validate_input(val):
37+
print('Invalid input, input again...')
38+
val = input('Input for {0}: '.format(player)).lower()
39+
40+
return val
41+
42+
43+
def compare(input_a, input_b):
44+
if input_a == input_b:
45+
result = 0
46+
elif rule[input_b] == input_a:
47+
result = 1
48+
else:
49+
result = 2
50+
return result
51+
52+
def game():
53+
play_flag = 'yes'
54+
while not play_flag=='quit':
55+
print('Choices for input: rock, paper, scissors')
56+
input_a = valid_input_factory('playerA')
57+
input_b = valid_input_factory('playerB')
58+
59+
print('player_A: {0}'.format(input_a), 'player_B: {0}'.format(input_b))
60+
61+
res = compare(input_a, input_b)
62+
print(results[res])
63+
64+
play_flag = input('Press <quit> to quit the game. \nPress any thing else to continue...')
65+
66+
67+
if __name__ == '__main__':
68+
game()
69+

0 commit comments

Comments
 (0)