HJ3 明明的随机数
count = int(input())
num_list = []
for i in range(count):
num = int(input())
if num not in num_list:
num_list.append(num)
for i in sorted(num_list):
print(i)
手写一个快排
import random
def qsort(num_list:list):
if len(num_list) < 2:
return num_list
else:
pivot = random.choice(num_list)
left = [num for num in num_list if num <= pivot]
right = [num for num in num_list if num > pivot]
return qsort(left) + qsort(right)
count = int(input())
num_list = []
for i in range(count):
num = int(input())
if num not in num_list:
num_list.append(num)
for i in qsort(num_list):
print(i)
HJ0 字符个数统计
str = input()
char_list = []
for char in str:
if char not in char_list:
char_list.append(char)
print(len(char_list))
HJ7 坐标移动
class Coordinate:
def __init__(self) -> None:
self.x = 0
self.y = 0
self.move_operator = list("ADWS")
def order_is_legal(self, move_order):
if move_order[:1] not in self.move_operator:
return False
try:
int(move_order[1:])
return True
except:
return False
def execute_move_order(self, move_order):
if self.order_is_legal(move_order):
distance = int(move_order[1:])
direction = move_order[:1]
if direction == "A":
self.x -= distance
elif direction == "D":
self.x += distance
elif direction == "W":
self.y += distance
elif direction == "S":
self.y -= distance
move_order_str= input()
move_order_list = move_order_str.split(";")
coordinate = Coordinate()
for move_order in move_order_list:
coordinate.execute_move_order(move_order)
print(f"{coordinate.x},{coordinate.y}")
HJ106 字符串逆序
多熟悉熟悉Python切片
input_str = input()
print(input_str[::-1])
HJ33.整数与IP地址间的转换
def num2ip(num):
ip = ""
while num > 256:
ans = num % 256
if not ip:
ip = str(ans)
else:
ip = f"{str(ans)}.{ip}"
num = num // 256
return f"{num}.{ip}"
def ip2num(ip):
num_list = ip.split(".")
ans = 0
for num in num_list:
ans = ans*256 + int(num)
return ans
ip = str(input())
print(ip2num(ip))
num = int(input())
print(num2ip(num))
HJ8.合并表记录
length = int(input())
num_dict = {}
for i in range(length):
line = input()
nums = line.split(" ")
if num_dict.get(int(nums[0])):
num_dict[int(nums[0])] += int(nums[1])
else:
num_dict[int(nums[0])] = int(nums[1])
for key in sorted(num_dict.keys()):
print(key, num_dict[key])
- 原来字典的建可以是数字啊。2.给键排序输出就好了
HJ14.字符串排序
num = int(input())
str_list = []
for i in range(num):
str_list.append(str(input()))
for string in sorted(str_list):
print(string)
OR26
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
#
# @param A string字符串
# @return int整型
#
class Solution:
def getLongestPalindrome(self , A: str) -> int:
size = len(A)
array = [[1 if i==j else 0 for i in range(size)] for j in range(size)]
max = 1
for length in range(1, size):
for head in range(size-length):
if length == 1:
if A[head] == A[head+length]:
array[head][head+length] = 1
max = 2 if 2 > max else max
else:
if array[head+1][head+length-1]:
if A[head] == A[head+length]:
array[head][head+length] = 1
max = length+1 if length+1 > max else max
return max
好好想想递推式