xonecas.io
387
Pe

python-exercices / commit d22a552

Add 3 new exercise solutions, from 13, 14, and 15.

xonecas2020-08-24 d22a55280f17df7f0579bc7317deed29b0e6cba7 parent 42b641a

3 files changed

level_two/ex_13.py+30−0
@@ -0,0 +1,30 @@
1+#!/usr/bin/env python3
2+
3+
4+# Question 13
5+# Level 2
6+
7+# Question:
8+# Write a program that accepts a sentence and calculate the number of letters and digits.
9+# Suppose the following input is supplied to the program:
10+# hello world! 123
11+# Then, the output should be:
12+# LETTERS 10
13+# DIGITS 3
14+
15+# Hints:
16+# In case of input data being supplied to the question, it should be assumed to be a console input.
17+
18+
19+sentence = input("Enter a sentence with letters and digits:\n")
20+digits_count = 0
21+letters_count = 0
22+
23+for c in sentence:
24+ if c.isdigit():
25+ digits_count = digits_count + 1
26+ elif c.isalpha():
27+ letters_count = letters_count + 1
28+
29+print("LETTERS: " + str(letters_count))
30+print("DIGITS: " + str(digits_count))
level_two/ex_14.py+31−0
@@ -0,0 +1,31 @@
1+#!/usr/bin/env python3
2+
3+
4+# Question 14
5+# Level 2
6+
7+# Question:
8+# Write a program that accepts a sentence and calculate the number of upper case letters
9+# and lower case letters.
10+# Suppose the following input is supplied to the program:
11+# Hello world!
12+# Then, the output should be:
13+# UPPER CASE 1
14+# LOWER CASE 9
15+
16+# Hints:
17+# In case of input data being supplied to the question, it should be assumed to be a console input.
18+
19+
20+sentence = input("Enter a sentence with upper and lower case letters:\n")
21+upper_count = 0
22+lower_count = 0
23+
24+for c in sentence:
25+ if c.isupper():
26+ upper_count = upper_count + 1
27+ elif c.islower():
28+ lower_count = lower_count + 1
29+
30+print("UPPER CASE", upper_count)
31+print("LOWER CASE", lower_count)
level_two/ex_15.py+24−0
@@ -0,0 +1,24 @@
1+#!/usr/bin/env python3
2+
3+# Question 15
4+# Level 2
5+
6+# Question:
7+# Write a program that computes the value of a+aa+aaa+aaaa with a given digit as the value of a.
8+# Suppose the following input is supplied to the program:
9+# 9
10+# Then, the output should be:
11+# 11106
12+
13+# Hints:
14+# In case of input data being supplied to the question, it should be assumed to be a console input.
15+
16+
17+number = input('Please enter a single number:\n')
18+result = 0
19+
20+for i in range(1, 5):
21+ new_number = number * i # number is a str here, it should produce a srquence of the same char
22+ result = int(new_number) + result
23+
24+print(result)