Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the high-frequency interview questions for Python?

2025-03-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "what are the Python high-frequency interview questions?" in the operation of actual cases, many people will encounter such a dilemma, and then let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

one。 Given an array of integers

Given an array of integers and a target value, find out the two numbers in the array that sum to the target value. You can assume that each input corresponds to only one answer, and that the same element cannot be reused. Example: given nums = [2, 7, 7, 11, 15], target=9 returns [0, 1] because nums [0] + nums [1] = 2: 7 = 9

Class Solution: def twoSum (self,nums,target): "": type nums: List [int]: type target: int: rtype: List [int] "" d = {} size = 0 while size

< len(nums): if target-nums[size] in d: if d[target-nums[size]] 0 and len(l2)>

0: if L1 [0] 0: tmp.append (L1 [0]) del L1 [0] while len (L2) > 0: tmp.append (L2 [0]) del L2 [0] return tmp

nine。 Given an array of arbitrary length, implement a function

Let all odd numbers precede even numbers, and odd numbers are arranged in ascending order, and even numbers are sorted in descending order, such as the string '1982376455' becomes' 1355798642'

# method 1: def func1 (l): if isinstance (l, str): l = [int (I) for i in l] l.sort (reverse=True) for i in range (len (l)): if l [I]% 2 > 0: l.insert (0, l.pop (I)) print (''.join (str (e) for e in l)) # method II def func2 (l): print (".join (sorted (l)) Key=lambda x: int (x)% 2 = 0 and 20-int (x) or int (x)

ten。 Write a function to find the second largest number in an array of integers

Def find_second_large_num (num_list): "" find the second largest number in the array "" # method 1 # sort directly, and output the penultimate number tmp_list = sorted (num_list) print ("method 1\ nSecond_large_num is:", tmp_list [- 2]) # method 2 # sets two flag bits, one storage maximum number and one storage secondary number # two storage second largest value One stores the maximum value, and you can traverse the array once. First, determine whether it is greater than one. If it is greater than, give the value of one to two, give the value of num_ [I] to one, otherwise compare whether it is greater than two, and if greater than give the value of num_ list [I] directly to two. Otherwise pass one = num_list [0] two = num_list [0] for i in range (1, len (num_list)): if num_ list [I] > one: two = one one = num_ list [I] elif num_ list [I] > two: two = num_ list [I] print ("method 2\ nSecond_large_num is:", two) # method 3 # use reduce and logical symbols (and, or) # basic ideas are the same as method two But you don't need to use if to judge. From functools import reduce num = reduce (lambda ot, x: ot [1] < x and (ot [1], x) or ot [0] < x and (x, ot [1]) or ot, num_list, (0,0)) [0] print ("method three\ nSecond_large_num is:", num) if _ name__ = ='_ main___': num_list = [34, 11, 23, 56, 78, 0, 9, 12, 3, 7 5] this is the end of the introduction of find_second_large_num (num_list) "what are the high-frequency interview questions for Python"? Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report