Skip to content

Commit 0fc44e0

Browse files
committed
Create 100 python exercises - ex15.py
1 parent 81b147a commit 0fc44e0

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

100 python exercises - ex15.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Question 15
2+
# Level 2
3+
#
4+
# Question:
5+
# Write a program that computes the value of a+aa+aaa+aaaa
6+
# with a given digit as the value of a.
7+
8+
# Suppose the following input is supplied to the program:
9+
# 9
10+
# Then, the output should be:
11+
# 11106
12+
#
13+
14+
# Function:
15+
# 1 - Ask a digit
16+
# 2 - Ask how many time it should be added (2--> a + aa / 4--> a + aa + aaa + aaaa)
17+
18+
19+
def conv(digit, repeats):
20+
start = range(1,repeats+1)
21+
sequence = []
22+
count = 0
23+
for p in start:
24+
sequence.append(int(str(digit) * p))
25+
for p in sequence:
26+
count = count + p
27+
return count
28+
29+
digit = int(raw_input("digit? "))
30+
repeats = int(raw_input("repeats? "))
31+
32+
print conv(digit,repeats)
33+
34+
# Book's Solution:
35+
# a = raw_input()
36+
# n1 = int( "%s" % a )
37+
# n2 = int( "%s%s" % (a,a) )
38+
# n3 = int( "%s%s%s" % (a,a,a) )
39+
# n4 = int( "%s%s%s%s" % (a,a,a,a) )
40+
# print n1+n2+n3+n4

0 commit comments

Comments
 (0)