<jupyter_start><jupyter_text>CWM AI Day1:Python<NAME> 2021/5/31 Ex3.1<jupyter_code>def get_info():
  info = []
  print("Please file in:")
  info += [input("name:")]
  info += [input("age:")]
  info += [input("job:")]
  print( info[0]+" is "+info[1]+" years old and works as a "+info[2])
  return 0

get_info()<jupyter_output>{'key1': 'val1 new', 'key2': 'val2 new'}<jupyter_text>Ex3.2<jupyter_code>def get_length(num):
  if type(num) == int:
    if num > 0:
      length = len(str(num))
      return length
    else:
      return 0
  else:
    return print("wrong type")

get_length(10)<jupyter_output><empty_output><jupyter_text>Ex3.3<jupyter_code>def get_over_hun(my_list): 
  new_list = [i for i in my_list if i>100]
  return new_list
  
#for check
# get_over_hun([1,500,88])<jupyter_output><empty_output><jupyter_text>Ex3.4<jupyter_code>def cap_d(uncap_dict):
  cap_dict={key:val.capitalize() for key,val in uncap_dict.items()}
  return cap_dict

# for check
# d1 = {"1":"haha","2":"nima"}
# cap_d(d1)<jupyter_output><empty_output><jupyter_text>Ex3.5<jupyter_code>def check_pali(nums):
  forward_num = str(nums)
  backward_num = forward_num[::-1]
  if forward_num == backward_num:
    return True
  else:
    return False

check_pali(898)<jupyter_output><empty_output><jupyter_text>Ex3.6<jupyter_code>def find_num(scope=[1000,1500]):
  list_num = []
  for i in range(1000,1501):
    if i % 7 == 0 and not i % 4 == 0:
        list_num += [str(i)]
    else:
      pass
  num_str = ",".join(list_num)
  return num_str

find_num()<jupyter_output><empty_output><jupyter_text>Ex3.7<jupyter_code>def anagram(num1,num2):
  lis1 = list(str(num1))
  lis2 = list(str(num2))
  for i in lis1:
    if i in lis2:
      lis1.remove(i)
      
  return lis1
  
anagram(998,996)<jupyter_output><empty_output><jupyter_text>Ex3.8<jupyter_code>def digit_convert(base,num):
  if base == '10': base = '0'
  base_mode = list('1234567890abcdefghijklmnopqrstuvwxyz')
  mode_dict = {}
  k = 1
  for i in base_mode:
    mode_dict[i] = k
    k+=1
  digit = list(str(num)[::-1])
  dec = 0
  mode = mode_dict[base]
  for i,k in enumerate(digit):
    dec += int(k) * mode**i
  return dec

digit_convert('2',111)<jupyter_output><empty_output><jupyter_text>Ex3.9<jupyter_code>def calculator(operation):
  op = operation.split(' ')
  a = int(op[0])
  b = int(op[-1])
  if op[1] == '+':return a+b
  elif op[1] == '-':return a-b
  elif op[1] == '*':return a*b
  elif op[1] == '÷':return a/b
  else: print('wrong operator')

calculator('1 * 1')<jupyter_output><empty_output><jupyter_text>Ex3.10<jupyter_code>#Recursion
def fibonacci_rec(n):
  if n == 0: return 0
  elif n == 1: return 1
  else: return fibonacci_rec(n-1)+fibonacci_rec(n-2)

fibonacci_rec(3)
#Iteration
def fibonacci_itr(n):
  i = 0
  fibo_pre = 0
  fibo = 1
  fibo_sum = 0
  while i < n-1:
    fibo_sum = fibo_pre + fibo
    fibo_pre,fibo = fibo,fibo_sum
    i += 1
  return fibo

fibonacci_itr(3)<jupyter_output><empty_output><jupyter_text>Ex3.11<jupyter_code>def<jupyter_output><empty_output><jupyter_text>Ex3.12<jupyter_code>def find_min(lis):
  min = lis[0]
  for i in lis:
    if i < min:
      min = i
  return min

lis = [7,10,-5,5,0]
find_min(lis)<jupyter_output><empty_output><jupyter_text>Ex3.13<jupyter_code>def find_max(lis):
  max = lis[0]
  for i in lis:
    if i > max:
      max = i
  return max

lis = [7,10,-5,5,0]
find_max(lis)<jupyter_output><empty_output><jupyter_text>Ex3.14<jupyter_code>def remove_vowel(word):
  vowels = list('aeiou')
  word_lis = list(str(word))
  for i in vowels:
    if i in word:
      word_lis.remove(i)

  return ''.join(word_lis)

remove_vowel("assdasdaraeafdfasd")<jupyter_output><empty_output><jupyter_text>Ex3.15<jupyter_code>def make_isotri(n):
  if n % 2 == 1:
    nrange = [i for i in range(n+1)]
    nums = nrange[1:-1:2] + [n]
    for i in nums:
      k = int((n-i)/2)
      print(' '*k+'*'*i+' '*k)
  else: return 0

make_isotri(5)<jupyter_output>*  
 *** 
*****<jupyter_text>Ex3.16<jupyter_code>def remove_neib(s):
  while 1:
    olen = len(s)
    i = 0
    while i < len(s)-1:
        if i >= 0 and s[i] == s[i+1]:
            s = s[:i]+s[i+2:]
            i -= 2
        i += 1 
    if olen == len(s):
        return s

remove_neib("aabba")<jupyter_output><empty_output><jupyter_text>Object Oriented Programming Ex4.1<jupyter_code>class Fruits:
  Description = 'a subclass of food'

  def __init__(self,taste):
    self.taste = taste
    self.size = 100

  def eat(self,volume):
    self.remain = False
    self.remain_size = self.size - volume

class Ball:
  def __init__(self):
    self.shape = 'sphere'
  def bounce():
    self.state = 'bouncing'

class Watermelon(Fruits,Ball):
  def __init__(self,taste):
    super().__init__(taste)
  
  
watermelon_bought_yesterday = Watermelon('sweet')
print(watermelon_bought_yesterday.taste)<jupyter_output>sweet<jupyter_text>Ex4.2<jupyter_code>#Non OOP
def place_circle(xlim,ylim,circle_list,x,y,r):
    flag = 0
    if x+r > xlim or x-r < 0 or y+r > ylim or y-r < 0:
      flag = 1
      print('False')
      return
    else:
      for element in circle_list:
        if (element[0]-x)**2 + (element[1]-y)**2 < (element[2]+r)**2:
          flag = 1
          print('False')
          return
          
    if flag == 0:
      print('True')
      circle_list.append([x,y,r])
      return<jupyter_output>Two circles have no intersection<jupyter_text>Ex4.3<jupyter_code>#OOP
class grid:
  def __init__(self,x,y):
    self.xlim = x
    self.ylim = y
    self.circle_list = []

  def place_circle(self,x,y,r):
    flag = 0
    if x+r > self.xlim or x-r < 0 or y+r > self.ylim or y-r < 0:
      flag = 1
      print('False')
      return
    else:
      for element in self.circle_list:
        if (element[0]-x)**2 + (element[1]-y)**2 < (element[2]+r)**2:
          flag = 1
          print('False')
          return
          
    if flag == 0:
      print('True')
      self.circle_list.append([x,y,r])
      return
grid1 = grid(10,10)
grid1.place_circle(2,2,2)
grid1.place_circle(2,3,3)<jupyter_output>True
False<jupyter_text>Ex4.4<jupyter_code>#n_dim
class ngrid:
  def __init__(self,dims):
    #n = [x,y,z....]
    self.lims = dims
    self.n = len(dims)
    self.circle_list = []

  def place_circle(self,c_dims,r):
    flag = 0
    for i in self.n:
      if c_dims[i]+r > self.lims[i] or c_dims[i]-r < 0:
        flag = 1
        print('False')
        return
      else:
        for element in self.circle_list:
          for k in len(c_dims):
            sum += (element[k]-c_dims[k])**2 

          if sum < (element[-1]+r)**2:
            flag = 1
            print('False')
            return
   
          
    if flag == 0:
      print('True')
      circle_list.append([c_dims,r])
      return<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1546757275816221, 'fraction_numerical': 0.03575121879154971, 'mean_word_length': 3.2525125628140703, 'pattern_counts': {'":': 2, '<': 88, '<?xml version=': 0, '>': 86, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '20', 'dir': 'jupyter-structured-clean-dedup', 'id': '656447', 'license': 'MIT', 'n_tokens_mistral': 3006, 'n_tokens_neox': 2843, 'n_words': 535, 'path': 'CWM_AI_D1_Jianhao_Yuan.ipynb', 'repo_name': 'YuanJianhao508/YuanJianhao508.github.io', 'size': '6775'}