repl.it
@yskov/

practice python.org problems

Python

No description

fork
loading
Files
  • main.py
main.py
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
#excercise 3
'''
a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]


b= list()

for x in a: 
  if x<5:
     b.append(x)

c = list()
print (b)

print ([x for x in a if x<5])

num= int(input("Choose number:"))

for x in a: 
  if x< num:
    c.append(x)

print (c)

'''

'''
explanation for the above code: A list comprehension behaves like this: [output] for [item] in [list] if [filter]

As you can see there are 4 components in its syntax:
output, item, list and filter.

In the case of Sachin's code [aa for aa in a if aa < 5]:

output = aa
item = aa
list = a
filter = aa < 5

What this means is that I'm outputting the variable 'aa' which refers to each item in the list (a).

Since 'aa' is set to refer to each item in list (a), the output will print the items in list (a). However, I also have a filter specified at the end of the code "if aa < 5".
'''

#excercise 6
#https://www.practicepython.org/exercise/2014/03/12/06-string-lists.html
'''
r = input("choose a string:")

count= -1
y= 0
for x in r:
  print (x)
  if x == r[count]:
    y = y+1
    print ('yes')
    

count = count - 1
print (y)
if y*2 == len(r):
  print ("This is a palindrome")
'''

#the above code was what I tried, but the solution on the website showed a better way where you can just use the [::-1] monicker to reverse the word and then set the original equal to the eversal

'''
wrd=input("Please enter a word")
wrd=str(wrd)
rvs=wrd[::-1]
print(rvs)
if wrd == rvs:
    print("This word is a palindrome")
else:
    print("This word is not a palindrome")

#good explanation can be seen here: https://stackoverflow.com/questions/53716324/what-does-1-mean-in-python

  

  
'''
  
#excercise 5. https://www.practicepython.org/exercise/2014/03/05/05-list-overlap.html

''''
c = list()

a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
b = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]

for x in a:
  if x in b:
    c.append(x)

print (c)


#this creates a random list of 10 items. 
import random
d= random.sample(range(1,100), 10)

print (d)

'''

#https://www.practicepython.org/exercise/2014/03/19/07-list-comprehensions.html

'''

b= list()
a = [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

for x in a:
  if x%2 == 0:
    b.append(x)


print(b)

#the above is my code, the below is a faster code

a = [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
e = [n for n in a if n % 2 == 0]
print(e,end="")

'''
'''
#create a program to put in a list die throws and log those throws if a die is thrown 30 times in a list. 

import numpy as np

n = 6
k = 3

a = np.arange(1,n) #Creating my initial list

b= []



count = 0
while count<31:
  numbers = np.random.choice(a,1)
  x = numbers[0]
  b.append(x)
  count = count+1


print (b)

'''

'''
#https://www.practicepython.org/exercise/2014/02/05/02-odd-or-even.html
x = input("Give a number")

x = int(x)

if x%2 ==0 and x%4 !=0:
  print("This is an even number")
elif x%2 !=0: 
  print ("This is an odd number")
if x%4 == 0:
  print("This is a multiple of 4")
'''

'''
#https://www.practicepython.org/solution/2014/04/02/08-rock-paper-scissors-solutions.html

game = "y"

while game == "y":

  x = input ("Your choice")
  y= input ("Your choice 2")

  if x == y:
    print ("draw")
    game= input ("do you want to play another?")
 

  while x or y == "Rock":
    if x or y == "Scissors":
      print("Rock smashes scissors")
      game= input ("do you want to play another?")
      
  while x or y == "Scissors":
    if x or y == "Paper":
      print("scissors cuts paper!")
    elif x or y == "Rock":
      print ("Rock smashes scissors!")

  while x or y =="Paper":
    if x or y =="Scissors":
      print("Scissors cuts paper!")
    elif x or y == "Rock":
      print("Rock breks Scissors!")

'''

#excercise 9


'''
import random

a = random.randint(1, 9)
b = 0
print(a)



c = 0

while b != a:
  b = input ("What is your guess?")
  

  if b =="exit":
    break
  b = int(b)
  if b == a: 
    print ("That is correct!")
  else:
    print ("That is not correct")
  c = c+1
  print ("Number of guesses", c)
 
'''



'''

def fibonacci(x):
  

  

  count = 0
  r = 1

  c = [1]
  while count< x:
    
    r = c[count-1]+c[count]
    count = count +1 
    c.append(r)
    
  return (c)
    


#https://www.practicepython.org/exercise/2014/05/21/15-reverse-word-order.html

#takes the array of a string and passes it into the join method to reverse the string

x = input("Please give me a string? ")

result= x.split()

print (result)


  

newresult= " ".join(word for word in result[::-1])



print (newresult)

'''
  
'''
#https://www.practicepython.org/exercise/2014/02/26/04-divisors.html

x= input ("Insert a number? ")

x= int(x)

y = range (1, x+1)

z = list()

for i in y:
  if x%i == 0:
    z.append(i)
    


print (z)

#the above code is mine, but the below code is a way faster implementation: 

num = int(input("choose a number: "))
x = list(range(1, num+1))
print([i for i in x if num % i == 0])

'''


#LIST COMPREHENSION VERY IMPORTANT
#https://www.practicepython.org/solution/2014/04/16/10-list-overlap-comprehensions-solutions.html


a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
b = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]

#this was the solution using list comprehension in one line of code that instructs to create a new list with the duplicates included. 

result = [i for i in a if i in b]

print (result)