Skip to content

Commit cbc0398

Browse files
Toy_problems
1 parent 5fda599 commit cbc0398

File tree

3 files changed

+100
-0
lines changed

3 files changed

+100
-0
lines changed

Cut_sticks.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
'''
2+
Problem Statement
3+
4+
You are given N sticks, where the length of each stick is a positive
5+
integer. A cut operation is performed on the sticks such that all of
6+
them are reduced by the length of the smallest stick.
7+
8+
Suppose we have six sticks of the following lengths:
9+
10+
5 4 4 2 2 8
11+
12+
Then, in one cut operation we make a cut of length 2 from each of the
13+
six sticks. For the next cut operation four sticks are left (of non-zero
14+
length), whose lengths are the following:
15+
16+
3 2 2 6
17+
18+
The above step is repeated until no sticks are left.
19+
20+
Given the length of N sticks, print the number of sticks that are cut
21+
in subsequent cut operations.
22+
23+
Note: For each cut operation, you have to recalcuate the length of
24+
smallest sticks (excluding zero-length sticks).
25+
26+
Input Format
27+
The first line contains a single integer N.
28+
The next line contains N integers: a0, a1,...aN-1 separated by space,
29+
where ai represents the length of ith stick.
30+
31+
Output Format
32+
For each operation, print the number of sticks that are cut in separate
33+
line.
34+
35+
Constraints
36+
1 ≤ N ≤ 1000
37+
1 ≤ ai ≤ 1000
38+
'''
39+
40+
def cut_sticks(sticks):
41+
new_sticks = []
42+
while len(sticks) > 0:
43+
new_sticks = []
44+
cuts = 0
45+
minV = min(sticks)
46+
for i in xrange(len(sticks)):
47+
sticks[i] -= minV
48+
cuts += 1
49+
if sticks[i] != 0:
50+
new_sticks.append(sticks[i])
51+
sticks = new_sticks
52+
print cuts
53+
54+
if __name__ == '__main__':
55+
in1 = raw_input()
56+
sticks = map(int, raw_input().strip().split(" "))
57+
cut_sticks(sticks)

FizzBuzzShort.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
'''Problem Statement
2+
3+
Write a program that prints (to STDOUT) the numbers from 1 to 100. But for multiples of three print "Fizz" instead of the number and for the multiples of five print "Buzz". For numbers which are multiples of both three and five print "FizzBuzz".
4+
5+
The goal is to write the shortest code possible.
6+
'''
7+
8+
9+
for i in range(1,101):
10+
print ('FizzBuzz' if i%15==0 else ('Buzz' if i%5==0 else \
11+
('Fizz' if i%3==0 else i) ) )

Lonely_Integer.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
'''
2+
Problem Statement:
3+
4+
There are N integers in an array A. All but one integer occur in pairs. Your task is to find the number that occurs only once.
5+
6+
Input Format
7+
8+
The first line of the input contains an integer N, indicating the number of integers. The next line contains N space-separated integers that form the array A.
9+
10+
Constraints
11+
12+
1≤N<100
13+
N % 2=1 (N is an odd number)
14+
0≤A[i]≤100,∀i∈[1,N]
15+
Output Format
16+
17+
Output S, the number that occurs only once.
18+
'''
19+
20+
from collections import Counter
21+
def lonelyinteger(a):
22+
c = Counter(a)
23+
answer = 0
24+
for i in c:
25+
if c[i] == 1:
26+
answer = i
27+
return answer
28+
29+
if __name__ == '__main__':
30+
a = input()
31+
b = map(int, raw_input().strip().split(" "))
32+
print lonelyinteger(b)

0 commit comments

Comments
 (0)