# Sample Input 0 # 10 3 # 2 4 # 6 7 # 9 9 # get_sweetness_levels(10, 3, [[2,4], [6,7], [9,9]]) def get_sweetness_levels(bar_length, student_count, student_lists): sweetness_level_list = [] # Create the chocolate bar chocolate_bar = [x for x in range(0, bar_length)] # Start giving chocolates to the students ''' student_count or length of student_lists can be used for number of students ''' for student in student_lists: left = student[0] right = student[1] student_sv = [] # Try and see if he/she gets bonus chocolates # Check Left if chocolate_bar.index(left) != 0: student_sv.append(chocolate_bar[chocolate_bar.index(left) - 1]) # Check Right if chocolate_bar.index(right) != len(chocolate_bar) - 1: student_sv.append(chocolate_bar[chocolate_bar.index(right) + 1]) # Get students choice for choice in range(left, right + 1): student_sv.append(choice) # Compute for sweetness level sweetness_level_list.append(sum(student_sv)) # Give chocolate to student chocolate_bar = [x for x in chocolate_bar if x not in student_sv] return sweetness_level_list get_sweetness_levels(10, 3, [[2,4], [6,7], [9,9]])
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