Algorithm Learning (4), algorithm Learning

Source: Internet
Author: User

Algorithm Learning (4), algorithm Learning
1. Bit Count (Bit calculation)

Note: All values in the computer are represented in binary systems. In this simple task, you need to write a program that calculates the non-zero digits in the given value. We use 32-bit integer values. Therefore, we should use 0 to 32 non-zero digits.

value             binary                count  1   00000000000000000000000000000001      1100   00000000000000000000000001100100      3 -1   11111111111111111111111111111111     32

That is, the value is converted to a 32-bit binary value and the number of values 1 is calculated.

Input dataContains the number of processes.
The second row contains the numbers to be processed. Their range is-2 000 000 000To2 000 000 000.
AnswerContains 1 digits, separated by spaces.

input data:31 100 -1answer:1 3 32

The Code is as follows:

1 nums = [1,100,-1] 2 3 def num_of_one (num): 4 nbin = bin (num & 0 xffffffff) # convert to a 32-bit binary 5 return nbin. count ('1') # calculate the number of digits of 1 6 7 for num in nums: 8 print (num_of_one (num), end = '')
# Output 1 3 32

 

2. Matching Brackets (Matching Brackets)

Note: The string we get contains four types of brackets (), square [], braces {}, and angle <>. Our goal is to check whether the order of parentheses is correct. That is to say, any square brackets should have an ending square brackets of the same type in another place of the string, and the square brackets should not overlap, although they can be nested.

(A + [B * c]-{d/3})-square brackets and braces are embedded in circular brackets (a + [B * c) -17]-square brackets and circles overlap, which is meaningless.

Input dataContains the quantity to be checked.
One test case per line.
AnswerReturns 1 (if the order of parentheses is correct), returns 0 (if incorrect), separated by spaces.

input data:4(a+[b*c]-{d/3})(a + [b * c) - 17](((a * x) + [b] * y) + cauf(zlo)men [gy<psy>] four{s}answer:1 0 0 1

The Code is as follows:

1 Arrays = ['(a + [B * c]-{d/3})', 2' (a + [B * c)-17] ', 3' (a * x) + [B] * y) + C', 4' auf (zlo) men [gy <psy>] four {s} '] 5 6 SYMBOLS = {'} ':' {','] ':' [',') ': '(', '>': '<'} 7 SYMBOLS_L, SYMBOLS_R = SYMBOLS. values (), SYMBOLS. keys () 8 9 def check (s): 10 arr = [] 11 for c in s: 12 if c in SYMBOLS_L: 13 # left symbol into stack 14 arr. append (c) 15 elif c in SYMBOLS_R: 16 # The right symbol either goes out of the stack or fails to match. 17 if arr and arr [-1] = SYMBOLS [c]: 18 arr. pop () # match the appropriate 19 else: 20 return '0' 21 return not arr 22 23 24 for I in range (len (Arrays )): 25 if check (Arrays [I]) is True: 26 print ('1', end = '') 27 else: 28 print ('0', end = '')

 

 

 

 

 

 

 

 

 

 

 

 

 

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.