-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathstarter_code.py
More file actions
118 lines (91 loc) · 3.21 KB
/
starter_code.py
File metadata and controls
118 lines (91 loc) · 3.21 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
# Recommended imports
import math # For doing math
import collections # For things like Counters and defaultdict's
import string # For things like getting all the ASCII chars, or alphabet
# Before the competition, become familiar with whats in these three modules
# Also, sometimes theres complex math (imaginary numbers!)
import cmath # Like math but works for imaginary numbers too!
# To write imaginary numbers, write a + bj == (a + bi)
z = 3+4j # Is equal to 3+4i
z.imag # -> 4
z.real # -> 3
# Some useful functions - ord, chr
print(ord('a')) # -> 65
print(chr(65)) # -> 'a'
# Converts between ASCII (or Unicode) value and the character
# Getting input from standard input
# Use these functions when reading input from console
# input() to read a full line (without the newline at the end)
ipt = input() # <- John
print("Hi, {}!".format(ipt)) # -> Hi, John!
def readint() -> int:
"""
Read an integer from the input, will error if the input is not an int
For example `5`
"""
return int(input())
def readfloat() -> float:
"""
Read a float from the input, will error if the input is not a float
For example `5`, `.3`, `3.`, and `4.35`
"""
return float(input())
def readlist(delimiter: str =" ") -> list:
"""
Read a list of items split by the `delimiter`, which is by default a space.
To use a comma as a delimiter use `readlist(",")`
For example `John Roy Kevin` -> ["John", "Roy", "Kevin"]
"""
return input().split(delimiter)
def readlistint(delimiter: str=" "):
"""
Read a list of integers split by the `delimiter`, see `readlist`
For example `5 4 3 1 2` -> [5, 4, 3, 1, 2]
"""
return [int(x.strip()) for x in input().split(delimiter)]
def readlistfloat(delimiter: str=" "):
"""
Read a list of integers split by the `delimiter`, see `readlist`
For example `5 4.1 3.21 1. .2` -> [5, 4.1, 3.21, 1., .2]
"""
return [float(x.strip()) for x in input().split(delimiter)]
# When reading input from a file
# For example, at CodeQuest the input files match `ProbXX.in.txt`
# Versus the file names which are `ProbXX.py`
ending = ".in.txt"
filename = __file__.split("/")[-1].split(".")[0] + ending
# If this file is called `ProbXX.py`, filename will be `ProbXX.in.txt`
dfile = open(filename, 'r')
# To use the above functions with the new file instead
# Redefine the input() function as follows:
def input():
return dfile.readline()[:-1]
# At CodeQuest all problems will give input as
# First: a number denoting the number of test cases `n`
# Second: n number of lines for the inputs
# For example:
# 2
# Henry
# John
for case in range(readint()):
name = input()
print("Hello, {}!".format(name)) #(or if you're really advanced `f"Hello, {name}"`
# Hello, Henry!
# Hello, John!
# Some will include sub-cases
# For example, with sums
# 2 - number of cases
# 3 - number of numbers to sum
# 6 - the numbers to sum
# 32
# 517
# 2 - second set of numbers
# 82
# 12
for case in range(readint()):
sum = 0
for nnum in range(readint()):
sum += readint()
print("The sum is", sum)
# The sum is 555
# The sum is 94