Python determines the maximum number of consecutive 0 or 1

Source: Internet
Author: User

Posted on someone asked, from the terminal read into an integer n, random one input a 0 or 1

Determine the maximum number of consecutive 0 or 1. Such as:

Input

0

0

0

1

1

1

1

0

1

0

1 in continuous input, there are 4 times


#coding: Utf-8

"" Python Beijing Weekend training Course

Https://github.com/pythonpeixun/article/blob/master/beijing_weekend.md

Python Shanghai Weekend Course

Https://github.com/pythonpeixun/article/blob/master/shanghai_weekend.md

Inquiry: Written by qq:1465376564 Huanggo

The idea of doing this exercise is to add 0 or 1 to a list with an n cycle first,

Finally, a dictionary is used to count the maximum number of consecutive 0 or 1 occurrences.


"""


Input_lst = []

Dict_num = {}

n = Int (raw_input ("Please input n:\n"). Strip ())

For I in Xrange (n):

Number = Int (raw_input ("Please input number:\n"). Strip ())

Input_lst.append (number)

length = Len (input_lst)

For I, item in enumerate (INPUT_LST):

If 0 < i < length:

If input_lst[i] = = Input_lst[i-1]:

If item in Dict_num:

Dict_num[item] + = 1

Else

Dict_num[item] = 1

Print Input_lst

Print Max (Dict_num.values ())


Python determines the maximum number of consecutive 0 or 1

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.