-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpractice.py
More file actions
162 lines (145 loc) · 6.07 KB
/
practice.py
File metadata and controls
162 lines (145 loc) · 6.07 KB
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
import collections.abc
from math import sqrt
from collections import deque
import string
import random
"""
Write a short Python function, is_multiple(n, m), that takes two integer values and returns True if n is a multiple of m, that is, n = mi for some integer i, and False otherwise.
"""
def is_multiple(n,m):
try:
return n/m == n//m
except ZeroDivisionError:
return "error caught"
"""Write a short Python function, is_even(k), that takes an integer value and returns True if k is even, and False otherwise. However, your function cannot use the multiplication, modulo, or division operators.
"""
def is_even(k):
try:
return not (k & 1)
except TypeError:
return "error caught"
"""Write a short Python function, minmax(data), that takes a sequence of one or more numbers, and returns the smallest and largest numbers, in the form of a tuple of length two. Do not use the built-in functions min or max in implementing your solution.
"""
def minmax(data):
if not isinstance(data, collections.abc.Iterable):
raise TypeError('data must be an iterable type')
min, max = None, None
for value in data:
if type(value) is not int and type(value) is not float:
continue
if min == None:
min, max = value, value
if (value < min):
min = value
if (value > max):
max = value
return min, max
"""Write a short Python function that takes a positive integer n and returns the sum of the squares of all the positive integers smaller than n.
"""
def sqr_sum(n):
sum = 0
if type(n) is not int:
return sum
for k in range(1, n):
sum += k * k
return sum
def sqr_sum_oneline(n):
return sum(k * k for k in range(1, n))
"""
Write a short Python function that takes a positive integer n and returns the sum of the squares of all the odd positive integers smaller than n.
"""
def sqr_odd_sum(n):
if type(n) is not int:
return 0
return sum(k * k for k in range(1, n) if k & 1)
"""Python's random module includes a function choice(data) that returns a random element from a non-empty sequence. The random module includes a more basic function randrange, with parameterization similar to the built-in range function, that return a random choice from the given range. Using only the randrange function, implement your own version of the choice function.
"""
def ran_choice(data):
random.seed(103)
return data[random.randrange(0,len(data))]
"""Write a pseudo-code description of a function that reverses a list of n integers, so that the numbers are listed in the opposite order than they were before, and compare this method to an equivalent Python function for doing the same thing.
"""
def rev_int_list(data):
j, k = 0, len(data)-1
while (j < k):
data[j], data[k] = data[k], data[j]
j += 1
k -= 1
"""
Write a short Python function that takes a sequence of integer values and determines if there is a distinct pair of numbers in the sequence whose product is odd.
"""
def distinc_pair_odd_product(data):
#find two distinct odd then product will be odd
first_odd = 0
for i in data:
if i & 1:
if not (first_odd & 1):
first_odd = i
if first_odd != i:
return True
return False
"""
Write a Python function that takes a sequence of numbers and determines if all the numbers are different from each other (that is, they are distinct).
"""
def all_distinct(data):
if not isinstance(data, collections.abc.Iterable):
raise TypeError('data must be an iterable type')
try:
list.sort(data)
except TypeError:
return "error caught"
if len(data) < 2:
return True
i = 0
for j in range (1, len(data)):
if data[i] == data[j]:
return False
i += 1
return True
"""
Python's random module includes a function shuffle(data) that accepts a list of elements and randomly reorders the elements so that each possible order occurs with equal probability. The random module includes a more basic function randint(a, b) that returns auniformly random integer from a to b (including both endpoints). Using only the randint function, implement your own version of the shuffle function.
"""
def my_shuffle(data):
random.seed(0)
upper_bound = len(data)
for i in range(0, upper_bound):
swap_i = random.randint(0,upper_bound-1)
data[i], data[swap_i] = data[swap_i], data[i]
"""Write a Python program that repeatedly reads lines from standard input until an EOFError is raised, and then outputs those lines in reverse order (a user can indicate end of input by typing ctrl-D).
"""
def reverse_input():
s = deque()
try:
while True:
s.append(input("enter something\n"))
except EOFError:
try:
while True:
print(s.pop())
except IndexError:
print("finished")
"""Write a short Python program that takes two arrays a and b of length n storing int values, and returns the dot product of a and b. That is, it returns an array c of length n such that c[i] = a[i] · b[i], for i = 0,. . ., n – 1.
"""
def dot_product(a, b):
if len(a) != len(b):
return 0
return [a[i] * b[i] for i in range(len(a))]
"""Write a short Python function that counts the number of vowels in a given character string.
"""
def count_vowels(str):
#casefold to ignore case
str = str.casefold()
vowels = 'aeiou'
count = 0
for chr in str:
if chr in vowels:
count += 1
return count
"""Write a short Python function that takes a string s, representing a sentence, and returns a copy of the string with all punctuation removed. For example, if given the string “Let's try, Mike.”, this function would return “Lets try Mike”.
"""
def remove_punctuations(str):
return "".join(c for c in str if c not in string.punctuation)
if __name__ == "__main__":
pow_of_two = [pow(2,k) for k in range(10)]
pronic = [n * (n + 1) for n in range (10)]
a_to_z = [chr(c) for c in range (ord('a'), ord('z') + 1)]