-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
117 lines (88 loc) · 2.73 KB
/
main.py
File metadata and controls
117 lines (88 loc) · 2.73 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
# hw1: CSCD 330, homework 1.
# author: Kenton Bell
from sys import argv
from random import seed, randint
def main():
# Do not edit main.
if len(argv) != 4:
print("Usage:")
print("\tpython3 hw1.py seed count max")
exit(1)
# Convert from string to int.
seed_value, element_count, max_value = map(int, argv[1:])
seed(seed_value) # Output must be reproducible.
values = []
for _ in range(element_count):
values += [randint(0, max_value)]
sum_every_nth(values, randint(1, max_value))
count_duplicates(values)
text = ""
line_count = 0
with open("Frankenstein.txt", "r") as file:
for line in file: # Do not modify.
text += line
line_count += 1
print_line_after(text, randint(0, line_count - 1),
randint(0, max_value))
def sum_every_nth(values, n):
"""
Prints out the sum of every nth element of values, including
the 0th element.
For example:
- ([1, 2, 3, 4], 2) => 4
- ([3, 5], 7) => 3
Inputs:
- values (int[]): A list of ints.
- n (int): An int to step by.
Outputs:
- None (Output is printed to STDOUT)
"""
# TODO: Use a for loop and the range function.
# print(sum(values[::n])) #this works too
total = 0
for i in range(0, len(values), n):
total += values[i]
print(total)
pass
def count_duplicates(values):
"""
Prints out a sorted list of [(value, occurence_count)] calculated
from the input values.
For example:
- ([1, 2, 3]) => [(1, 1), (2, 1), (3, 1)]
- ([1, 2, 3, 2, 1]) => [(1, 2), (2, 2), (3, 1)]
Inputs:
- values (int): A list of ints.
Outputs:
- None (Output is printed to STDOUT)
"""
duplicates = {}
# TODO: Use a dictionary and a for loop.
for i in values:
if i in duplicates:
duplicates[i] += 1
else:
duplicates[i] = 1
print(sorted(duplicates.items()))
def print_line_after(text, line, after):
"""
Print the line from text starting at the index 'after'.
For example:
- ("Hello, world!", 0, 7) => world!
- ("1\n2\n3", 1, 0) => 2
- ("1\n2\n3", 1, 5) =>
Inputs:
- text (String): A string of characters.
- line (int): An int selecting the line from the text as
separated by the new line character '\n'
- after (int): An int selecting the starting index on the line
to print. Inclusive.
Outputs:
- None (Output is printed to STDOUT)
"""
# TODO: Use the split function.
lines = text.split('\n')
print(lines[line][after:])
pass
if __name__ == "__main__":
main()