2018-05-29 Python

2018-05-29  本文已影响0人  HenrySHE

i.cs.hku.hk/~yklam2/py

Link2 (ref)

2018.5.29 Night.

Number base

image.png
y = int(input("Input a year: "))
if (y%4 == 0 and y%100 != 0):
    print("yes it is a leap year")
else:
    if y%400 ==0:
        print("yes it is a leap year")
    else:
        print("It is not leap year")
j = int(input("input a number: "))
while j>0 :
    print(j%10)
    j //=10
n = int(input("input a number: "))
t = [0,1]
for i in range(0,n-2):
    t.append(t[i]+t[i+1])
print(t)  

3. Data Structure

image.png
image.png
image.png image.png
month = int(input("Input a Month of a day: "))
first_day = int(input("Input a the first day is: "))
result = []
if (first_day ==0):
    a = 1
    result.append(a)
    a=8
    while (a < month):
        result.append(a)
        result.append(a+1)
        a = a+7
else:
    a = 7-first_day
    while (a < month+6):
        result.append(a)
        result.append(a+1)
        a = a+7

print(result)

#------------- Another Method-----------------------

month = int(input("Input a Month of a day: "))
first_day = int(input("Input a the first day is: "))
result = []
for i in range(month):
    dow = (i + first_day) %7
    if dow==6 or dow ==0:
        result.append(i+1)
print(result)

Tuple

image.png
image.png
image.png
image.png
image.png image.png
a = int(input("Input a: "))
b = int(input("Input b: "))
while (b != 0):
    a,b = b,a%b
print(a)
image.png
b = 1
result = []
while (b != 0 ):
    b = int(input("Input b: "))
    if b == 0:
        break
    result.append(b)

print (result)
for i in range(1,(len(result)//2)+1):
    print (result[i:-i])
#-------------[Method 2]
b = 1
result = []
while (b != 0 ):
    b = int(input("Input b: "))
    result.append(b)

for i in range(0,(len(result)//2)):
    print (result[i:-(i+1)])
image.png image.png image.png
s = input("Input string: ")
s = (s*2)
for i in range(0,len(s)//2):
    if i==0:
        print(s)
    else:
        print(" "*i + s[i:-i])
image.png
s = input("Input string: ")
result = ""
for i in range (1,len(s)+1):
    result = result + s[:i]
for i in range (1,len(s)+1):
    result = result + s[i:]
    
print(result)
def myrange(a = "" ,b = "" ,c = "" ):
    if(a=="" and b=="" and c==""):
        print("no value is passing in")
    if (a!="" and b!= "" and c!=""):
        print ([a,b,c])
        return [a,b,c]
    if(a!="" and b!= ""):
        print ([a,b])
        return [a,b]
    if(a!=""):
        print([a])
        return(a)
        

myrange()
myrange(1,2,3)
myrange(1,2)
myrange(1)

Chapter 5 Exercises 2

image.png
image.png
import random

#Random Generate Methods
def randDot():
    x = random.random()
    y = random.random()
    return (x*x + y*y <1)
    
#Find Pi
def findPi(n):
    count = 0
    for i in range(n):
        if randDot():
            count+=1
    return 4 * count/n

print(findPi(1000000))
image.png

Constructer

class Box:
    def __init__(self, w, h, d):
        self.w, self.h, self.d = w, h, d
        
    def getVolume(self):
        return self.w * self.h * self.d

b = Box(10, 20, 30)
print(b.getVolume())
c = b
b.w = 22
print(c.getVolume())
c.w = 66
print(b.getVolume())

'''
Results:
6000
13200
39600
39600
'''

Exercise of Class

image
import math

class Frac:
    _n = 1
    _d = 1
    def __init__(self,n,d):
        self._n = n
        self._d = d
    def __str__(self):
        self._simplify()
        return f'{self._n:d}/{self._d:d}'
    
    def _simplify(self):
       gcd = math.gcd(self._n,self._d)
       self._n //= gcd
       self._d //= gcd
        
    def invert(self):
        return f'{self._n}/{self._d}'

f1 = Frac(2,4)
f2 = Frac(6,3)
print(f1,f2)

Other Exercises:

import math

class Frac:
    _n = 1
    _d = 1
    def __init__(self,n,d):
        self._n = n
        self._d = d
        
    def __str__(self):
        self._simplify()
        return f'{self._n:d}/{self._d:d}'
    
    def _simplify(self):
       gcd = math.gcd(self._n,self._d)
       self._n //= gcd
       self._d //= gcd
    
    def invert(self):
        self._d,self._n=self._n,self._d

    def multi(self,f):
        self._n *= f._n
        self._d *= f._d
    
    #Still has some problems, not simply add them together 
    def add(self,f):
        self._n += f._n
        self._d += f._d
    
    def eval(self):
        return self._n/self._d

x = Frac(1,3)
y = Frac(1,2)
x.add(x)
print(x)
x.add(y)
上一篇 下一篇

猜你喜欢

热点阅读