Skip to content

Commit b9bee3e

Browse files
committed
Completed more exerecises
1 parent ec62ae9 commit b9bee3e

File tree

6 files changed

+110
-0
lines changed

6 files changed

+110
-0
lines changed

exercises/exercise-013.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Write a program that accepts a sentence and calculate the number of letters and digits.
2+
# Suppose the following input is supplied to the program:
3+
# hello world! 123
4+
# Then, the output should be:
5+
# LETTERS 10
6+
# DIGITS 3
7+
8+
raw = input('Write something: ')
9+
stats = { 'digits': 0, 'letters': 0 }
10+
11+
for char in raw:
12+
if char.isdigit():
13+
stats['digits']+=1
14+
elif char.isalpha():
15+
stats['letters']+=1
16+
else:
17+
pass
18+
19+
print("LETTERS:", stats['letters'])
20+
print("NUMBERS:", stats['digits'])
21+

exercises/exercise-014.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Write a program that accepts a sentence and calculate the number of upper case letters and lower case letters.
2+
# Suppose the following input is supplied to the program:
3+
# Hello world!
4+
# Then, the output should be:
5+
# UPPER CASE 1
6+
# LOWER CASE 9
7+
8+
raw = input("Write something:")
9+
10+
stats = { 'upper': 0, 'lower': 0 }
11+
12+
for char in raw:
13+
if char.isupper():
14+
stats['upper'] += 1
15+
elif char.islower():
16+
stats['lower'] += 1
17+
else:
18+
pass
19+
20+
print('UPPER: ', stats['upper'])
21+
print('LOWER: ', stats['lower'])
22+

exercises/exercise-015.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Write a program that computes the value of a+aa+aaa+aaaa with a given digit as the value of a.
2+
# Suppose the following input is supplied to the program:
3+
# 9
4+
# Then, the output should be:
5+
# 11106
6+
7+
raw = input('Write a number: ')
8+
9+
a = int('%s' % (raw))
10+
aa = int('%s%s' % (raw, raw))
11+
aaa = int('%s%s%s' % (raw, raw, raw))
12+
aaaa = int('%s%s%s%s' % (raw, raw, raw, raw))
13+
14+
print(a + aa + aaa + aaaa)

exercises/exercise-016.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Use a list comprehension to square each odd number in a list. The list is input by a sequence of comma-separated numbers.
2+
# Suppose the following input is supplied to the program:
3+
# 1, 2, 3, 4, 5, 6, 7, 8, 9
4+
# Then, the output should be:
5+
# 1, 3, 5, 7, 9
6+
7+
raw = input('Write a serie of numbers: ')
8+
9+
numbers = [i for i in raw.split(',') if int(i) % 2 != 0]
10+
11+
print(','.join(numbers))

exercises/exercise-017.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# 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:
2+
# D 100
3+
# W 200
4+
# D means deposit while W means withdrawal.
5+
# Suppose the following input is supplied to the program:
6+
# D 300
7+
# D 300
8+
# W 200
9+
# D 100
10+
# Then, the output should be:
11+
# 500
12+
13+
amount = 0
14+
while True:
15+
raw = input("Make a transaction: ")
16+
if not raw:
17+
break
18+
args = raw.split(' ')
19+
operation = args[0]
20+
value = float(args[1])
21+
if operation is 'D':
22+
amount += value
23+
elif operation is 'W':
24+
amount -= value
25+
else:
26+
pass
27+
28+
print('Total amount:', amount)

exercises/exercise-018.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# A website requires the users to input username and password to register. Write a program to check the validity of password input by users.
2+
# Following are the criteria for checking the password:
3+
# 1. At least 1 letter between [a-z]
4+
# 2. At least 1 number between [0-9]
5+
# 1. At least 1 letter between [A-Z]
6+
# 3. At least 1 character from [$#@]
7+
# 4. Minimum length of transaction password: 6
8+
# 5. Maximum length of transaction password: 12
9+
# Your 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.
10+
# Example
11+
# If the following passwords are given as input to the program:
12+
# ABd1234@1,a F1#,2w3E*,2We3345
13+
# Then, the output of the program should be:
14+
# ABd1234@1

0 commit comments

Comments
 (0)