程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Huawei computer test (Python) of Niuke network

編輯:Python

Preface

Huawei machine test of Niuke network (https://www.nowcoder.com/exam/oj/ta?tpId=37)
According to the difficulty, it can be divided into introductory , Simple , secondary , difficult , Five levels of difficulty

Table of contents title

  • Preface
  • One , introduction
  • Two , Simple

One , introduction

HJ7 Take an approximation

n = float(input())
res = lambda x:int(x+0.5)
print(res(n))

HJ9 Extract non duplicate integers
After extracting the input string, invert it , When sorting, directly press ls1 To arrange in order

ls1 = list(input()[::-1])
ls2 = list(set(ls1))
ls2.sort(key=ls1.index)
print(int(''.join(ls2)))

HJ46 Intercepting string
String slice

str1 = input()
n = int(input())
print(str1[:n])

HJ58 Input n It's an integer , Output the smallest of them k individual
map() Function is to initialize n,k And in the list str The type is int type
The last output *nums[:k] It's right num[:k] Unpacking .

n, k = map(int, input().split())
nums = sorted(list(map(int, input().split())))
print(*nums[:k])

HJ101 Enter an integer array and sort ID , Sort its elements in ascending or descending order
Is still map() Function conversion type ,sort() Function order ,* Unpack list .

n = int(input())
ls1 = list(map(int,input().split()))
flag = int(input())
if flag == 0:
ls1.sort()
print(*ls1)
elif flag == 1:
ls1.sort(reverse=True)
print(*ls1)

Two , Simple

HJ1 The length of the last word in the string

ls1 = input().split()
print(len(ls1[-1]))

HJ2 Count the number of occurrences of a character
count function

str1 = input()
str2 = input()
print(str1.lower().count(str2.lower()))

Counter class

from collections import Counter
str1 = input().lower()
str2 = input().lower()
s = Counter(str1)
if str2 in str1:
print(s.get(str2))
else:
print('0')

for loop

str1 = input().lower()
str2 = input().lower()
count = 0
for i in str1:
if str2 == i:
count+=1
print(count)

HJ4 String delimitation
ljust() Function is a fill function ,str1.ljust(8,“0”) It means to let str1 Align left , Insufficient 8 The remaining bits are used 0 fill

str1 = input()
while len(str1)>8:
print(str1[:8])
str1 = str1[8:]
print(str1.ljust(8,"0"))

HJ5 Hexadecimal conversion
Hexadecimal conversion 10 Base number

str1 = input()
print(int(str1,16))

HJ6 Prime factor
Find the prime factor of an integer ,180 = 22335,2 2 3 3 5 Namely 180 The quality factor of
1 No qualitative factor ,2 The prime factor of is itself
math.sqrt(n) Is a number squared , The minimum prime number of a number is 2, The maximum does not exceed the square root
% It's surplus ,// It's a division

import math
n = int(input())
for i in range(2,int(math.sqrt(n))+1):
while n%i == 0:
print(i,end=' ')
n = n//i
if n >= 2:
print(n,end=' ')

HJ8 Consolidated statement records
dicc[i] = dicc.get(i,0) + j It means to dicc The key i Value , If it cannot be obtained, it means that the key does not exist and the default value is obtained 0, therefore dicc[i] = 0+j . If key i There is , Take out the value if it is 10, So give it again dicc[i] assignment 10+j
sorted(dict) The operation dictionary type also returns a list , But the default is an incrementing list of keys .

num = int(input())
dicc = {
}
for n in range(num):
i,j = list(map(int,input().split()))
dicc[i] = dicc.get(i,0) + j
for k in sorted(dicc):
print(f'{
k} {
dicc[k]}')

HJ10 Character count

str1 = input()
print(len(set(str1.replace('\n',''))))

HJ11 The numbers are upside down
Number inversion is easy to do with string slicing
The following is an array , The idea is to calculate str(num) The length of , Add the characters after the string to the front of the list , Then the output

nums = str(input())
i = len(nums) - 1
ans = []
while i>=0:
ans.append(nums[i])
i-=1
res = ''.join(ans)
print(res)

HJ12 String inversion

str1 = str(input())
print(str1[::-1])

HJ13 The sentence is in reverse order
Slice inverted list

list = input().split()
print(' '.join(list[::-1]))

reserve() Functions invert the list

list = input().split()
list.reverse()
print(' '.join(list))

HJ14 String sort

n = int(input())
lst = []
for i in range(n):
lst.append(str(input()))
lst.sort()
for i in lst:
print(i)

HJ15 seek int When a type positive integer is stored in memory 1 The number of
This is actually a decimal conversion binary , Then find the number of occurrences of a character in the string

n = int(input())
n = bin(n)
print(n.count('1'))

Of course, you can also change the method of base conversion and the method of character occurrence times

n = int(input())
lst = []
while n != 0:
lst.append(n%2)
n = n//2
count = 0
for i in lst:
if i==1:
count+=1
print(count)

HJ21 Simple code

str1 = str(input())
dicc = {

'abc':2,
'def':3,
'ghi':4,
'jkl':5,
'mno':6,
'pqrs':7,
'tuv':8,
'wxyz':9,
}
lst = []
for i in str1:
if i.isdigit():
lst.append(int(i))
elif i.isupper():
if i == "Z":
i = "a
else:
i = chr(ord(i.lower())+1)
lst.append(i)
elif i.islower():
for j in dicc.keys():
if str(i) in j:
i = dicc[j]
lst.append(i)
print(''.join(map(str,lst)))

HJ22 Water bottle
Two soda bottles can also be exchanged for one , Finally, there will be no water bottle left .
Ideas : Take two empty bottles , Borrow an empty bottle from a merchant , Change a bottle of soda , After drinking, return an empty bottle to the merchant , It is equivalent to exchanging two empty bottles for a bottle of soda , And got nothing .

for i in range(10):
n = int(input())
if n==0:
break
print(n//2)

Three soda bottles can be exchanged for one , Then get an empty bottle , Finally, the remaining two soda bottles can be replaced with a bottle of soda .
The following idea is : Every time I change three empty bottles for a bottle of soda , Then you get an empty bottle , Calculate the final total number of empty bottles , Then the cycle , Finally, replace the remaining two water bottles , Otherwise, the result of the cycle will be output directly .


#count_1 Change to a soda 
#count_2 Water bottle 
#count_3 The soda you drink 
for i in range(10):
count_3 = 0
n = int(input())
if n == 0:
break
while n > 2:
count_1 = n//3
count_2 = n%3
count_3 = count_3 + count_1
n = count_1 + count_2
if n == 2:
count_3+=1
print(count_3)

HJ23 Delete the least frequent character in the string
First find the number of character occurrences , Remove the minimum number of characters , Then re output the characters as strings in the original order .
Ideas : Find the number of character occurrences , Then write it into the dictionary , Find the minimum value of the dictionary value , Loop the original string , Except for the least number of characters , Everything else outputs .

str1 = input()
lst = set(list(str1))
dicc = {
}
for i in lst:
dicc[i] = str1.count(i)
MIN = min(dicc.values())
res = ''
for i in str1:
if dicc[i] != MIN:
res += i
print(res)

HJ31 Word inversion
Match the words in the string to the list , Then turn it upside down join() Method becomes a string output .
Ideas : Regular expressions match words , Then invert it

import re
str1 = input()
lst1 = re.findall(r'\b[a-zA-Z]+\b',str1)
print(*lst1[::-1])
#print(*lst1[::-1]) Unpacking method 

HJ34 Picture arrangement

str1 = input()
lst = list(str1)
lst.sort(key=ord)
print(''.join(lst))

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved