This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution: | |
def construct2DArray(self, original: List[int], m: int, n: int) -> List[List[int]]: | |
if len(original) == m * n: | |
return [original[i*n:(i+1)*n] for i in range(m)] | |
else: | |
return [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution: | |
def isHappy(self, n: int) -> bool: | |
cycle = [] | |
while n not in cycle and n != 1: | |
cycle.append(n) | |
n = sum(map(lambda x: int(x)**2, str(n))) | |
return True if n == 1 else False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
MID = 9 | |
for i in range(MID): | |
row = [' '] * (2 * MID + 1) | |
row[MID - i] = row[MID + i] = '*' | |
print(''.join(row)) | |
print('*' * (2 * MID + 1)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 男生人數和女生人數初始值皆設為0 | |
N_males = N_females = 0 | |
with open('read.dat') as f: | |
for line in f.readlines(): | |
print(line) | |
N = line.split()[-2] | |
# 排除檔案第一行切出來的N為'性別'的情況 | |
if N == '0': | |
N_females += 1 | |
if N == '1': |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 開檔,每一行的每一個字存入words | |
words = [] | |
with open(input()) as f: | |
for line in f.readlines(): | |
words += line.split() | |
# 若用words.append()方法 | |
# 會將line.split()傳出的串列 | |
# 當成一個東西加到words最後一項 | |
# 一一比對words中的每一個word長度是否為n |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
with open(input()) as f: | |
old = input() | |
new = input() | |
outlines = [] | |
print('=== Before the replacement') | |
for readline in f.readlines(): | |
print(readline.strip()) | |
outlines.append(readline.replace(old, new)) | |
print('=== After the replacement') | |
for outline in outlines: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
names, heights, weights = [], [], [] | |
with open('read.txt') as f: | |
for line in f.readlines(): | |
print(line) | |
name, height, weight = line.split() | |
names.append(name) | |
heights.append(float(height)) | |
weights.append(float(weight)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
with open('read.txt') as f: | |
print(sum(map(int, f.readline().split()))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
for _ in range(int(input())): | |
# map是數學的映射的意思,map函數的用法是 | |
# map(轉換函數, 原始串列)傳出map物件 | |
# map物件轉list物件,即串列 | |
# 串列中的每一項就是原始串列中的每一項一一透過轉換函數得來 | |
numbers = list(map(float, input().split())) | |
# 差值 = 最大值 - 最小值 | |
answer = max(numbers) - min(numbers) | |
# 差值輸出到小數點後第二位 | |
print(f'{answer:.2f}') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
try: # 試試 | |
# 切成三段 | |
ddd, dd, dddd = input().split('-') | |
# 都轉整數 | |
int(ddd) | |
int(dd) | |
int(dddd) | |
# 若還沒炸 | |
print('Valid SSN') | |
except: # 有炸的話 |