Skip to content

Commit

Permalink
learn python
Browse files Browse the repository at this point in the history
  • Loading branch information
sword-jin committed Mar 16, 2016
1 parent 6fe294e commit d7fd83b
Show file tree
Hide file tree
Showing 42 changed files with 713 additions and 337 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/DataStructure/.idea
/DataStructure
/.idea
26 changes: 26 additions & 0 deletions Python/Data Types/1-python-lists.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/usr/bin/python

t = int(raw_input())

nums = []

for i in range(0, t):
orgs = raw_input().split(' ')
if orgs[0] == "insert":
nums.insert(int(orgs[1]), int(orgs[2]))
elif orgs[0] == "append":
nums.append(int(orgs[1]))
elif orgs[0] == "remove":
nums.remove(int(orgs[1]))
elif orgs[0] == "pop":
nums.pop()
elif orgs[0] == "index":
print nums.index(int(orgs[1]))
elif orgs[0] == "count":
print nums.count(int(orgs[1]))
elif orgs[0] == "sort":
nums.sort()
elif orgs[0] == "reverse":
nums.reverse()
elif orgs[0] == "print":
print nums
10 changes: 10 additions & 0 deletions Python/Data Types/2-python-tuples.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/usr/bin/python

from __builtin__ import hash

n = int(raw_input())
nums = map(int, raw_input().split(" "))

tuples = tuple(nums)

print hash(tuples)
11 changes: 11 additions & 0 deletions Python/Data Types/3-sets.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/usr/bin/python

m = int(raw_input())
one = set(map(int, raw_input().split()))
n = int(raw_input())
two = set(map(int, raw_input().split()))

result = one.symmetric_difference(two)

for i in sorted(result):
print i
5 changes: 5 additions & 0 deletions Python/Data Types/4-list-comprehensions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/python

x, y, z, n = (int(input()) for i in range(4))

print [[a, b, c] for a in range(0, x + 1) for b in range(0, y + 1) for c in range(0, z + 1) if a + b + c != n]
16 changes: 16 additions & 0 deletions Python/Data Types/5-find-second-maximum-number-in-a-list.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/usr/bin/python

n = int(raw_input())

nums = map(int, raw_input().split())

print max(filter(lambda x: x != max(nums), nums))

# just solution two.
# big, sbig = -6000, -6000
# for i in lis:
# if (i > big):
# big, sbig = i, big
# elif (i < big and i > sbig):
# sbig = i
# print (sbig)
32 changes: 32 additions & 0 deletions Python/Data Types/6-nested-list.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/usr/bin/python

a = [[raw_input(), float(raw_input())] for _ in range(int(raw_input()))]
s = sorted(set(x[1] for x in a))

for name in sorted(x[0] for x in a if x[1] == s[1]):
print name

# stupied
# n = int(raw_input())

# stus = []

# for _ in range(n):
# name = raw_input()
# grade = float(raw_input())
# stus.append([name, grade])

# stus.sort(lambda x, y: cmp(x[1], y[1]))
# print stus
# min = stus[0][1]
# stus = filter(lambda x: x[1] != min, stus)
# print stus
# min = stus[0][1]

# results = filter(lambda x: x[1] == min, stus)
# print stus
# results.sort(lambda x, y: cmp(x[0], y[0]))
# print stus

# for s in results:
# print s[0]
3 changes: 3 additions & 0 deletions Python/Introduction/1-py-hello-world.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/python

print("Hello World!")
6 changes: 6 additions & 0 deletions Python/Introduction/10-whats-your-name.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/python

firstname = raw_input()
lastname = raw_input()

print "Hello %s %s! You just delved into python." % (firstname, lastname)
13 changes: 13 additions & 0 deletions Python/Introduction/11-interchange-two-numbers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/python

a = int(raw_input())
b = int(raw_input())

temp = a
a = b
b = temp

num = (a, b)

print num[0]
print num[1]
15 changes: 15 additions & 0 deletions Python/Introduction/12-finding-the-percentage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/usr/bin/python

t = int(raw_input())

stus = {}

for i in range(0, t):
name, math, physic, chemistry = raw_input().split(" ")
stus[name] = (float(math) + float(physic) + float(chemistry)) / 3

name = raw_input()
if name in stus:
print("%.2f" % stus[name])
else:
print "No"
11 changes: 11 additions & 0 deletions Python/Introduction/13-python-print.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/usr/bin/python

# n = int(raw_input())

# print ''.join(map(str, range(1, n + 1)))
# print ''.join(str(x+1) for x in range(input()))

from __future__ import print_function

N = int(input())
print(*range(1, N + 1), sep='', end='\n', file=sys.stdout)
5 changes: 5 additions & 0 deletions Python/Introduction/2-python-raw-input.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/python

input = raw_input()

print(input)
8 changes: 8 additions & 0 deletions Python/Introduction/3-python-arithmetic-operators.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/python

one = int(raw_input())
two = int(raw_input())

print(one + two)
print(one - two)
print(one * two)
9 changes: 9 additions & 0 deletions Python/Introduction/4-python-division.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/python

from __future__ import division

one = int(raw_input())
two = int(raw_input())

print(one // two)
print(one / two)
11 changes: 11 additions & 0 deletions Python/Introduction/5-python-mod-divmod.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/usr/bin/python

from __future__ import division

# a = int(raw_input())
# b = int(raw_input())
a, b = int(raw_input(), raw_input())

print(a // b)
print(a % b)
print(divmod(a, b))
8 changes: 8 additions & 0 deletions Python/Introduction/6-python-power-mod-power.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/python

a = int(raw_input())
b = int(raw_input())
m = int(raw_input())

print(a ** b)
print(pow(a, b, m))
8 changes: 8 additions & 0 deletions Python/Introduction/7-python-integers-come-in-all-sizes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/python

a = int(raw_input())
b = int(raw_input())
m = int(raw_input())

print(a ** b)
print(pow(a, b, m))
8 changes: 8 additions & 0 deletions Python/Introduction/8-python-integers-come-in-all-sizes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/python

a = int(raw_input())
b = int(raw_input())
c = int(raw_input())
d = int(raw_input())

print(a**b + c**d)
6 changes: 6 additions & 0 deletions Python/Introduction/9-python-loops.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/python

a = int(raw_input())

for i in range(0, a):
print i ** 2
18 changes: 18 additions & 0 deletions Python/String/1-swap-case.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/usr/bin/python

from __future__ import print_function

s = raw_input()

for a in s:
if a.isalpha():
if a.islower():
print(a.upper(), end="")
else:
print(a.lower(), end="")
else:
print(a, end="")

# wtf!!!!
# import string
# print string.swapcase(raw_input())
12 changes: 12 additions & 0 deletions Python/String/10-python-string-formatting.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/usr/bin/python

n = int(raw_input())

width = len(bin(n)) - 2

for i in range(1, n + 1):
print "{0:{width}d} {0:{width}o} {0:{width}X} {0:{width}b}".format(i,width = width)

# from __future__ import print_function
# for i in range(1, n + 1):
# print("%s %s %s %s" % (str(i).rjust(width, " "), str(oct(i))[1:].rjust(width, " "), hex(i)[2:].rjust(width, " ").upper(), bin(i)[2:].rjust(width, " ")))
36 changes: 36 additions & 0 deletions Python/String/11-alphabet-rangoli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/usr/bin/python

# n = int(raw_input())
# for i in range(n):
# s = "-".join(chr(ord('a')+n-j-1) for j in range(i+1))
# print((s+s[::-1][1:]).center(n*4-3, '-'))

# for i in range(n-1):
# s = "-".join(chr(ord('a')+n-j-1) for j in range(n-i-1))
# print((s+s[::-1][1:]).center(n*4-3, '-'))

a = ord('a')
n = int(raw_input())
width = 4 * n - 3

str = []

for i in range(1, n):
left = ''
for j in range(i - 1):
left = left + chr(a + n - 1 - j) + '-'
right = left;
left = left + chr(a + n - i) + left[::-1]
str.append(left)

center = ''
for i in range(n - 1):
center = center + chr(a + n - 1 - i) + '-'
center = center + 'a' + center[::-1]

for s in str:
print s.center(width, '-')
print center.center(width, '-')

for s in reversed(str):
print s.center(width, '-')
3 changes: 3 additions & 0 deletions Python/String/12-capitalize.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/python

print ' '.join(word.capitalize() for word in raw_input().split(' '))
1 change: 1 addition & 0 deletions Python/String/13-the-minion-game.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#!/usr/bin/python
3 changes: 3 additions & 0 deletions Python/String/2-python-string-split-and-join.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/python

print "-".join(raw_input().split())
8 changes: 8 additions & 0 deletions Python/String/3-python-mutations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/local/python

s = raw_input()
args = raw_input().split()
pos = int(args[0])
char = args[1]

print s[:pos] + char + s[pos + 1:]
26 changes: 26 additions & 0 deletions Python/String/4-find-a-string.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/usr/bin/python

string = raw_input()
findme = raw_input()
count = 0
start = 0

while 1:
temp = string.find(findme, start)
if (temp != -1):
count = count + 1
start = temp + 1
else:
break

print count

# A = raw_input()
# x = raw_input()

# count = 0
# for i in range(len(A)-len(x)):

# if A[i:i+len(x)] == x:
# count += 1
# print count
Loading

0 comments on commit d7fd83b

Please sign in to comment.