1 introduction
2 variable, expression
3 if, loop
4 string, file
5 simple list
expression
type: integer, float, string
math
x in range()
c in ... string ...
s.find(c)
if cond: do_true else: do_false
while cond: do_body
for x in ...: do_body
input:
inp = input().strip()
rec = input().strip().split()
output
print()
expression
1 numeric
2 transform string
3 numeric + string
use input file
if
4 count the number of 1 in string {0,1}*
5 max of 3 using if
6 count inventory record
loop
7 input is two set of numbers, output all possible pairs (two nested
loops)
8 input is two strings of equal size, output is one string alternate
between each character of each input string.
9 input is a matrix represented by one row of numbers. write a
program to output its transpose using the similar representation.
1) sin x is defined as
sin x = x - x^3/3! + x^5/5! - x^7/7! + . . .
1.1) Write a program that input x and output sin x by calculating
according to this definition, using only up to x^7.
1.2) Compare the value of sin x in 1.1 with the value using math.sin(x)
hint: x unit is radian
use: import math
2) Input string is in the set {A,B,C,D,E,F,G,H} and the
corresponding output string is the set {X,Y,Z,I,J,K,L,M}. Write a program
that accepts the input string and transform it to the output string.
for example
input ACDF
output XZIK
hint:
get each character from input by
for c in in_string
get the index of c by
string.find(c)
3) input string is in the set {A,B,C,D,E,F,G,H} and the
corresponding value of each character is the set {1,2,3,4,5,6,7,8}. Write
a program that accepts the input string and change each character to its
value then output the sum of all values.
for example
input ACDF
output 14
hint: similar to 2) but index and get the value of each character
then sum it up.
use input file
4) Input is binary string of arbitrary lenght {0,1}*. Output
is the number of 1 in the string. The input file has 10
records.
input4.txt
input 100001111
output 5
hint: get each character from input-string.
5) Input is 3 numbers. Write a program using a decision tree
to find the max number. Each line of input file has 3 numbers. Output the
max number. Read input file until the end of file.
input5.txt
input 3 4 5
output 5
hint: You have to draw the decision tree, comparing two numbers at a
time.
to read until the end of file use
for line in infile:
6) The inventory consists of T-shirt with two colors {red,blue} and two
sizes {big,small}. The input file has the following form:
product_ID color size
1234 red big
1246 blue small
....
write a program to read the input file and count the inventory of each
category:
{red-big, red-small, blue-big, blue-small} and output that count.
for example
input6.txt
input
346 red big
2678 blue small
135 blue small
467 red big
output
2 0 0 2
hint:
to read until the end of file use
for line in infile:
to get each field in a line use
input().strip().split()
then access each field by index.
then use if..else to count each category.
7) two nested loop: input is two set of numbers, output all possible pairs
in the following format: read input until end of file
input7.txt
input
1 2 3
10 20 30
output
1 10 1 20 1 30
2 10 2 20 2 30
3 10 3 20 3 30
hint: keep each input in lists. using indexing the list to build
output.
rec = input.strip().split()
rec will be a list of each input field
index into rec[i] to get each number.
8) write a program to merger two strings. input is two strings
of equal size, output is one string alternate between each character of
each input string.
input8.txt
input
abcdefg
XYZIJKL
output
aXbYcZdIeJfKgL
hint: convert each input string into list. use index into each character
to merge into the output. Build the output string by concatenation
(operator + on string)
9) Transpose matrix 3 x 3. input is a matrix represented by
one row of numbers. write a program to output its transpose using
the similar representation.
input9.txt
input
1 2 3 4 5 6 7 8 9
is the matrix
1 2 3
4 5 6
7 8 9
output
1 4 7 2 5 8 3 6 9
which is the matrix
1 4 7
2 5 8
3 6 9
end
last update 9 Feb 2016