perm = ['+3', '+4', '+5', '-12', '-8', '-7', '-6', '+1', '+2', '+10', '+9', '-11', '+13', '+14'] def number_of_breakpoints(perm): ''' Find the number of breakpoints in a permutation. Input: A permutation. Output: The number of breakpoints in this permutation. ''' breakpoints = 0 if int(perm[0]) - 0 != 1: breakpoints += 1 for index in range(1, len(perm)): if int(perm[index]) - int(perm[index - 1]) != 1: breakpoints += 1 if len(perm) + 1 - int(perm[- 1]) != 1: breakpoints += 1 return breakpoints print number_of_breakpoints(perm)
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question