Tag: the sum () function provided by Python can accept a list and sum please write a prod () function to accept a list and use the reduce () to calculate the product:1 #Python提供的sum () function can accept a list and sum, write a prod () function, you can accept a list and use reduce () to calculate the product:Code:2 3 from Functools import reduce 4 Li = [1,2,3,4,5,7] 5 print (sum (LI)) 6 7 def prod (x, y): 8 return x * y 9 Ten R = Reduce (Prod,li) one print (R)Operation Result:[[email protected
60, the loop performs the add-on operation and counts the number of points - - - + - + A at //print output plus score, plus number of points - - - - - } in}1 Public classHelloWorld {2 Public Static voidMain (string[] args) {3 4 //Variable Save score5 intScore = 53; 6 7 //variables are saved in addition to the number of times8 intCount = 0;9 Ten One
[i],1)); + } A //get the number of occurrences of all characters theIte=Mymap.begin (); + for(i=0; I) - { $Chvalue[i]=ite->second; $ite++; - } - //sort the number of occurrences of all characters the sort (chvalue,mymap.size ()); - //Calculate the beauty of a nameWuyi for(i=0; I1; i++) {//I traverse the number of mymap.size ()-1, because the last character is a line break thenvalue+= (Chvalue[i]) *K; -k--; Wu } - returnNvalue; About } $ intMain () { -U
} -}The above code is a simple traversal, K is the size of n, when my k too big when the program crashes, can not solve 10e9 of the situation, so this problem cannot be solved with this idea. What to do, I think about given n when we can fall a few days according to it before and it goes up for a few days after the last fall, and the first thing I think about is the arithmetic progression summation formula, assuming there's no downside s=n+n (n-1)/2, find out that n is K days a total of
TopicsEnter a specific integer value and a set of integers that require two numbers to be found in this set of integers so that the sum of the two numbers equals a specific value. Outputs all two-digit positions that meet the criteria in the previous order. The specific format is as follows:InputOutput2 53 4Analysis1) Brute force solution, Complexity O (n^2), regardless of2) hash. Use hash table to store each number corresponding subscript, complexity O (n)3) First sort, then left and right clam
TopicsGiven n non-negative integers, which represent the height of the fence, calculate the amount of water the fence can hold for a given set of numbers.InputOutput6AnalysisFor each pillar, find the highest column on the left and right side, the column can accommodate the area is min (Max_left, max_right)-height. So you can scan it first, find the highest pillar, divide the array into two halves, and then handle it separately.Code#include #include using namespace STD;intMain () { vectorint>Bars
TopicsAssuming that n children stand in a row, each child is assigned an integer number. To give the children sugar according to the following requirements.1. Each child has at least one candy2, the number of children than the adjacent children to the candy to be moreAsk for at least a few candies to be issuedInput0 1 3 1 4Output9AnalysisCan cycle two times, the first time from the next, the next two children, the front number is larger than the number of sugar, the second pass from the back, th
= Argparse. Argumentparser ()Parser.add_argument (' DST ')args = Parser.parse_args ()DST = Args.dstBvffer = Process_file (DST)Word_freq = Process_buffer (Bvffer)Output_result (Word_freq)Second, Code style description1. Indent with 4 spaces2. Use blank lines to separate functions and classes, and large chunks of code within functions3. Spaces are used around the operator and after commas, but no space is added to the brackets4. Fold the line to make sure it doesn't exceed 79 charactersThird, the
and assigns an initial value6 int[] Nums =New int[] {61, 23, 4, 74, 13, 148, 20 };7 8 intmax = Nums[0];//assume that the maximum value is the first element in the array9 intmin = nums[0];//assume that the minimum value is the first element in the arrayTen Doublesum = 0;//Accumulated Value One DoubleAvg = 0;//Average A - for(inti = 0; i //iterate through the elements in an array - //if the current value is greater than
7-9. Translation(a) writing a character translation program (functionally similar to the TR command in Unix). We call this function tr (), which hasThree string parameters: source string, Destination string, basic string, syntax is defined as follows:def tr (Srcstr, DSTSTR, String)The content of Srcstr is the character set that you intend to "translate", Dsrstr is the character set that is obtained after translation, and string isThe string to which you intend to perform the translation operatio
reference type, when he is directly assigned to the value of the corresponding reference position is assigned to the string variable, so, two times the result is true. If you assign a value with the new String (), the result will be different.
Write a program that simulates the result of tossing a coinPackage Net.mindview.operators;import Java.util.random;importStaticnet.mindview.util.print.*; Public classThrowcron { Public Static voidMain (string[] args) {Random num=NewRandom (); intA = Num.
# -*- coding:utf-8 -*-#strip正则表达式#if 只字符串,不带参数,去首尾空白#else 去除参数text=input("Input a text:")import redef st(text): #原样返回检测 textReg=re.compile(r"^\w+$") #前后或前或后,空白检测 blankReg=re.compile(r‘^\s+(\w+)\s+$|^\s+(\w+)|(\w+)\s+$‘) check_blank=blankReg.search(text) #参数检测 argsReg=re.compile(r‘(\w+)\s+\.*‘) check_args=argsReg.search(text) if textReg.search(text): return(text) elif check_blank: #groups检测,直接通过groups输出 for i in check_blank.groups(): if i: return(i) elif check_args: return(check_args.group(1)) el
# -*- coding:utf-8 -*-# 7.18.1# 强口令检测# 写一个函数,使用正则表达式,确保传入的口令字符串是强口令# 长度不少于8个字符,同时包含大小写,至少有1个数字import repassd=input("Input your password:")ch_len=re.compile(r‘.{8,}‘)ch_uppercase=re.compile(r‘[A-Z]{1,}‘)ch_case=re.compile(r‘[a-z]{1,}‘)ch_number=re.compile(r‘\d{1,}‘)if ch_len.search(passd): if not ch_case.search(passd) or not ch_uppercase.search(passd) or not ch_number.search(passd) : print("弱密码") else: print("strong enough")else: print("length less then 8")"Python
#!python3# -*- coding:utf-8 -*-# 8.9.3#打开指定路径中所有.txt文件#用户输入正则,将匹配行输出,输出文件名import re,ospat=input("输入要处理的文件夹绝对路径:")lis_dir=os.listdir(pat)lis_txt=[]for x in lis_dir: #取出.txt结尾的文件名,加进列表 if re.search(r‘\.txt$‘,x): lis_txt.append(x)text=input("输入你的自定义正则表达式:")regCom=re.compile(text)#regCom=re.compile(‘.*name.*‘)#循环处理文件for fi in lis_txt: fi=os.path.join(pat,fi) fi_open=open(fi) for fi_line in fi_open.readlines(): fi_reg=regCom.search(fi_line) if fi_reg: print(fi_reg.group()) fi_open.close()print("查找完成"
numbers, ranging from large to small. (b) is the same as a, but it is arranged in a dictionary order from large to small. " # (a) Defget_num (): globalnum_listnum_list= []num= ' whilenum!= '! ': num=raw_input (' Enter some numbers to '! "End"). Strip () ifnum!= '! ': try: num=float (num) except: print ' input is wrong, please re-enter ' get_num () else: num_list.append (num) else: breakreturnnum_listdefsort_descending (): get_num () printsorted (num_list,reverse=true) print '---------------
Third questionPackage net.mindview.interfaces;Abstract classbase{ PublicBase () {print (); } Abstract voidprint ();} Public classTest3 extends base{Private inti =5; @Overridevoidprint () {System. out. println (i); } Public Static voidMain (string[] args) {Test3 T=NewTest3 (); T.print (); }}Output Result:0 5When calling the base class construction method, it simply allocates a chunk of memory space to the member variable of the subclass and sets the value of the memory space to the defa
1, write script/root/bin/systeminfo.sh, display the current host system information, including hostname, IPV4 address, operating system version, kernel version, CPU model, memory size, hard disk size2, scripting/root/bin/backup.sh, can be implemented daily/etc/directory backup to/root/etcyyyy-mm-dd3. Write script/root/bin/disk.sh, showing the maximum space utilization value in the current hard disk partition4. Write Script/root/bin/links.sh, show the IPV4 address and connection number of each re
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.