Quick actions

cmd+k|ctrl+k

Navigation

Languages

20240415

Snippet info

Language

Python

Visibility

public

Author

hkchun

Created

2024-04-15T02:49:35.505435Z

Updated

2024-04-15T05:09:27.723681Z

##1
#result = calculator(5,3,'+')
#print(result)
def calculator00(a, b, c):
    if c == '+':
        return a + b
    elif c == '-':
       return  a - b
    elif c == '*':
        return  a * b
    elif c == '/':
        if b == 0:
            return "Division error"
        else:
            return  a / b
    else:
        return "error"

result = calculator00(4, 0, '+')
print(result)

def calculator00(num1, num2, ope1):
    if ope1 =='+':
      return num1+num2
    elif ope1 =='-':
      return num1-num2
    elif ope1 =='*':
      return num1*num2
    elif ope1 =='/':
        if num2 ==0:
            return "Division error"
        else:
            return num1  / num2
    else:
        return "error : invalid operator"
result = calculator00(10, 10, '+')
print(result)
      
      
##2
########################################
#result =even_or_odd(7)
#print(result)

def evenorodd(a):
    return"odd"if int(a) %2 else "even"
    
result=evenorodd(7)
print(result)

##3
######################################
#TENET
#result=is_palindrome("radar")
#print(result)

def is_palindrome(string):
    return string == string[::-1]

result = is_palindrome("radar")
print(result)

##4
######################################
#1,1,2,3,5,8,13,21,
#result = filbonacci(8)
#print(result)

def fibonacci(n):
    fib_seq =[1,1]
    while len(fib_seq) < n: #len 長度
        fib_seq.append(fib_seq[-1]+fib_seq[-2]) #最尾和尾二
    return fib_seq
result = fibonacci(8)
print(result)

##5
######################################
#Factorial Calculator
#result = factorial(5)

"""" def factorial(n):
    if n==0:
        n=1
    else:
        return n* factorial(n-1)
result = factorial(5)
"""

def factorial(n):
    if n == 0:
        return 1
    else:
        return n * factorial(n - 1)

result = factorial(5)
print(result)
#6 CHECK 質數
"""is_prime()
result = is_prime()"""
def is_prime(num):
    if num <=1:
        return False
    for i in range(2,int(num)):
        if num % i ==0:
          return False
    return True
result = is_prime(97)
print (result)

#7
# list Reversal
# result = reverse_list([1,2,3,4,5])
def reverse_list(lst):
    return lst[::-1]

result = reverse_list([1, 2, 3, 4, 5])
print(result)

#8
''' String Reversal
result = reverse_string("hello")'''
def reverse_string(lst):
    return lst[::-1]

result = reverse_list("hello")
print(result)

#9
'''Duplicate Remover
def remove_duplicates(n):'''

def remove_duplicates(n):
    return list(set(n))
result = remove_duplicates([1,2,2,3,4,4,5])
print(result)

#10
'''Word Count
result = word_count("the quick brown fox jumps over the lazy dog")'''
def word_count(sentence):
    words = sentence.split()
    word_count_dict={}
    for word in words:
        if word in word_count_dict:
            word_count_dict[word] +=1
        else:
            word_count_dict[word] =1
    return word_count_dict
result = word_count("the quick brown fox jumps over the lazy dog")
print(result)
INFO