Codewars實戰(二)

題10 Regex validate PIN code

ATM machines allow 4 or 6 digit PIN codes and PIN codes cannot contain anything but exactly 4 digits or exactly 6 digits.
validate_pin(“1234”) == True
validate_pin(“12345”) == False
validate_pin(“a234”) == False

def validate_pin(pin):
    if len(pin) == 4 or len(pin) == 6:
        return pin.isdigit()
    else:
        return False
def validate_pin(pin):
    return len(pin) in (4, 6) and pin.isdigit()
# 判斷數字isdigit()
# 判斷字母isalpha()
# 判斷混合isalnum()

題11 Equal Sides Of An Array

You are going to be given an array of integers. Your job is to take that array and find an index N where the sum of the integers to the left of N is equal to the sum of the integers to the right of N. If there is no index that would make this happen, return -1.

def find_even_index(arr):
    for i in range(len(arr)):
        if sum(arr[:i]) == sum(arr[i+1:]):
            return i
        else:
            pass
    return -1
# 運用切片的方式       

題12 Simple Pig Latin

Move the first letter of each word to the end of it, then add “ay” to the end of the word. Leave punctuation marks untouched.
pig_it(‘Pig latin is cool’) # igPay atinlay siay oolcay
pig_it(‘Hello world !’) # elloHay orldway !

def pig_it(text):
    result = []
    wordlist = text.split()
    for each in wordlist:
        if each.isalpha():
            result.append(each[1:] + each[0] + 'ay')
        else:
            result.append(each)
    return ' '.join(result)
# 大佬方式
def pig_it(text):
    lst = text.split()
    return ' '.join( [word[1:] + word[:1] + 'ay' if word.isalpha() else word for word in lst])

題13 Calculating with Functions

This time we want to write calculations using functions and get the results

# 大佬的做法,使用了lambda,太強大了,要好好學它
import math
def zero(f = None):
    return 0 if not f else f(0)
def one(f = None): return 1 if not f else f(1)
def two(f = None): return 2 if not f else f(2)
def three(f = None): return 3 if not f else f(3)
def four(f = None): return 4 if not f else f(4)
def five(f = None): return 5 if not f else f(5)
def six(f = None): return 6 if not f else f(6)
def seven(f = None): return 7 if not f else f(7)
def eight(f = None): return 8 if not f else f(8)
def nine(f = None): return 9 if not f else f(9)
 
def plus(y): return lambda x: x+y
def minus(y): return lambda x: x-y
def times(y): return lambda  x: x*y
def divided_by(y): return lambda  x: math.floor(x/y)

題14 Create Phone Number

Write a function that accepts an array of 10 integers (between 0 and 9), that returns a string of those numbers in the form of a phone number.
create_phone_number([1, 2, 3, 4, 5, 6, 7, 8, 9, 0]) # => returns “(123) 456-7890”

# format值得擁有
def create_phone_number(n):
    n = ''.join(str(i) for i in n)
    return '({}) {}-{}'.format(n[:3], n[3:6], n[6:])
# 大佬做法
def create_phone_number(n):
  return "({}{}{}) {}{}{}-{}{}{}{}".format(*n)

題15 Valid Parentheses

Write a function called that takes a string of parentheses, and determines if the order of the parentheses is valid. The function should return true if the string is valid, and false if it’s invalid.
Examples
“()” => true
“)(()))” => false
“(” => false
“(())((()())())” => true

# 一開始不是很理解,一直有錯
def valid_parentheses(string):
    cnt = 0
    for char in string:
        if char == '(': cnt += 1
        if char == ')': cnt -= 1
        if cnt < 0: return False
    return True if cnt == 0 else False

題16 Find The Parity Outlier

You are given an array (which will have a length of at least 3, but could be very large) containing integers. The array is either entirely comprised of odd integers or entirely comprised of even integers except for a single integer N. Write a method that takes the array as an argument and returns this “outlier” N.
Examples
[2, 4, 0, 100, 4, 11, 2602, 36]
Should return: 11 (the only odd number)
[160, 3, 1719, 19, 11, 13, -21]
Should return: 160 (the only even number)

def find_outlier(integers):
    even = [i for i in integers if i % 2 == 0]
    odd = [i for i in integers if i % 2 != 0]
    if len(even) < len(odd):
        return even[0]
    else:
        return odd[0]
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章