Hackerrank New Year ChaosReverse Game HackerRank Ruby SolutionCircular Array Rotation C++...
Why do we call complex numbers “numbers” but we don’t consider 2-vectors numbers?
I am the light that shines in the dark
Is this Paypal Github SDK reference really a dangerous site?
What is better: yes / no radio, or simple checkbox?
Why do we say 'Pairwise Disjoint', rather than 'Disjoint'?
School performs periodic password audits. Is my password compromised?
What is the purpose of a disclaimer like "this is not legal advice"?
Why isn't P and P/poly trivially the same?
Having the player face themselves after the mid-game
What exactly is the meaning of "fine wine"?
How to write a chaotic neutral protagonist and prevent my readers from thinking they are evil?
What is Tony Stark injecting into himself in Iron Man 3?
Precision notation for voltmeters
Was this cameo in Captain Marvel computer generated?
Use Mercury as quenching liquid for swords?
What would be the most expensive material to an intergalactic society?
Why is my explanation wrong?
How to educate team mate to take screenshots for bugs with out unwanted stuff
Book where society has been split into 2 with a wall down the middle where one side embraced high tech whereas other side were totally against tech
Tabular environment - text vertically positions itself by bottom of tikz picture in adjacent cell
Should I file my taxes? No income, unemployed, but paid 2k in student loan interest
A running toilet that stops itself
Why would /etc/passwd be used every time someone executes `ls -l` command?
How to install "rounded" brake pads
Hackerrank New Year Chaos
Reverse Game HackerRank Ruby SolutionCircular Array Rotation C++ HackerRankHackerrank Sum vs XoRHackerrank Lower Bound-STLHackerrank Queen's Attack IIParallelogram ConnectivityHackerrank Modular Range QueriesHackerrank CTCI “Stacks: Balanced Brackets” Javascript SolutionCounting adjacent swaps to sort an array with 3 different valuesSolve Time Limit Exceeded for list<int> in c++
$begingroup$
My code given below will produce the correct output but it tends to take way too much time with larger data thus timing out for some of the test cases. I've linked to the problem description here:
It's New Year's Day and everyone's in line for the Wonderland rollercoaster ride!
There are n people queued up, and each person wears a sticker indicating their initial position in the queue (i.e. 1, 2, ..., n - 1, n: with the first number denoting the frontmost position).
Any person in the queue can bribe the person directly in front of them to swap positions. If two people swap positions, they still wear the same sticker denoting their original place in line. One person can bribe at most two other persons.
That is to say, if n = 8 and Person 5 bribes Person 4, the queue will look like this:
1, 2, 3, 5, 4, 6, 7, 8.
Fascinated by this chaotic queue, you decide you must know the minimum number of bribes that took place to get the queue into its current state!
Note: Each Person X wears sticker X, meaning they were initially the Xth person in queue.
Input Format
The first line contains an integer, T, denoting the number of test cases.
Each test case is comprised of two lines; the first line has n (an integer indicating the number of people in the queue), and the second line has n space-separated integers describing the final state of the queue.
Constraints
$$ 1 leq T leq 10 $$
$$ 1 leq n leq 10^5 $$
Subtasks
For 60% score $$1 leq n leq 10^3$$
For 100% score $$1 leq n leq 10^5$$
Output Format
Print an integer denoting the minimum number of bribes needed to get the queue into its final state; print Too chaotic if the state is invalid (requires Person X to bribe more than 2 people).
Sample Input
2
5
2 1 5 3 4
5
2 5 1 3 4
Sample Output
3
Too chaotic
Any help/advice/suggestion is welcome.
#!/bin/python3
import sys
inp0 = sys.stdin.read()
inp1 = inp0.split('n')
T = int(inp1[0])
del inp1[0]
# Swaps the i'th and (i-1)'th elements and returns the list
def swap(intermediate_q,i):
intermediate_q[i],intermediate_q[i-1] = intermediate_q[i-1],intermediate_q[i]
return(intermediate_q)
# Increment bribes and total_bribes
def compute_bribes(bribes,total_bribes):
if(bribes+1==3):
return (-1,-1)
return (bribes+1,total_bribes+1)
for i in range(0,(2*T)-1,2):
n = int(inp1[i])
final_q = list(int(i) for i in inp1[i+1].split(' '))
initial_q = list(i+1 for i in range(n))
total_bribes,chaos_flag=0,0
for i in range(n):
bribes = 0
# If the position in the initial/transition queue is not equal to the position in the final queue
# final_q is the input against which swaps are made in my initial_q.
if(final_q.index(initial_q[i]) != initial_q.index(initial_q[i])):
index_in_initial_q = initial_q.index(final_q[i])
index_in_final_q = final_q.index(final_q[i])
while(index_in_initial_q != index_in_final_q):
initial_q = swap(initial_q,index_in_initial_q)
bribes,total_bribes = compute_bribes(bribes,total_bribes)
if(bribes == -1):
print("Too chaotic")
chaos_flag=1
break
index_in_initial_q-=1
if(chaos_flag == 1):
break
if (chaos_flag == 1):
continue
print(total_bribes)
python performance programming-challenge python-3.x time-limit-exceeded
$endgroup$
add a comment |
$begingroup$
My code given below will produce the correct output but it tends to take way too much time with larger data thus timing out for some of the test cases. I've linked to the problem description here:
It's New Year's Day and everyone's in line for the Wonderland rollercoaster ride!
There are n people queued up, and each person wears a sticker indicating their initial position in the queue (i.e. 1, 2, ..., n - 1, n: with the first number denoting the frontmost position).
Any person in the queue can bribe the person directly in front of them to swap positions. If two people swap positions, they still wear the same sticker denoting their original place in line. One person can bribe at most two other persons.
That is to say, if n = 8 and Person 5 bribes Person 4, the queue will look like this:
1, 2, 3, 5, 4, 6, 7, 8.
Fascinated by this chaotic queue, you decide you must know the minimum number of bribes that took place to get the queue into its current state!
Note: Each Person X wears sticker X, meaning they were initially the Xth person in queue.
Input Format
The first line contains an integer, T, denoting the number of test cases.
Each test case is comprised of two lines; the first line has n (an integer indicating the number of people in the queue), and the second line has n space-separated integers describing the final state of the queue.
Constraints
$$ 1 leq T leq 10 $$
$$ 1 leq n leq 10^5 $$
Subtasks
For 60% score $$1 leq n leq 10^3$$
For 100% score $$1 leq n leq 10^5$$
Output Format
Print an integer denoting the minimum number of bribes needed to get the queue into its final state; print Too chaotic if the state is invalid (requires Person X to bribe more than 2 people).
Sample Input
2
5
2 1 5 3 4
5
2 5 1 3 4
Sample Output
3
Too chaotic
Any help/advice/suggestion is welcome.
#!/bin/python3
import sys
inp0 = sys.stdin.read()
inp1 = inp0.split('n')
T = int(inp1[0])
del inp1[0]
# Swaps the i'th and (i-1)'th elements and returns the list
def swap(intermediate_q,i):
intermediate_q[i],intermediate_q[i-1] = intermediate_q[i-1],intermediate_q[i]
return(intermediate_q)
# Increment bribes and total_bribes
def compute_bribes(bribes,total_bribes):
if(bribes+1==3):
return (-1,-1)
return (bribes+1,total_bribes+1)
for i in range(0,(2*T)-1,2):
n = int(inp1[i])
final_q = list(int(i) for i in inp1[i+1].split(' '))
initial_q = list(i+1 for i in range(n))
total_bribes,chaos_flag=0,0
for i in range(n):
bribes = 0
# If the position in the initial/transition queue is not equal to the position in the final queue
# final_q is the input against which swaps are made in my initial_q.
if(final_q.index(initial_q[i]) != initial_q.index(initial_q[i])):
index_in_initial_q = initial_q.index(final_q[i])
index_in_final_q = final_q.index(final_q[i])
while(index_in_initial_q != index_in_final_q):
initial_q = swap(initial_q,index_in_initial_q)
bribes,total_bribes = compute_bribes(bribes,total_bribes)
if(bribes == -1):
print("Too chaotic")
chaos_flag=1
break
index_in_initial_q-=1
if(chaos_flag == 1):
break
if (chaos_flag == 1):
continue
print(total_bribes)
python performance programming-challenge python-3.x time-limit-exceeded
$endgroup$
$begingroup$
There's a good chance you need a different algorithm. Possible approach: geeksforgeeks.org/counting-inversions
$endgroup$
– Robert Harvey
Jul 10 '16 at 15:20
add a comment |
$begingroup$
My code given below will produce the correct output but it tends to take way too much time with larger data thus timing out for some of the test cases. I've linked to the problem description here:
It's New Year's Day and everyone's in line for the Wonderland rollercoaster ride!
There are n people queued up, and each person wears a sticker indicating their initial position in the queue (i.e. 1, 2, ..., n - 1, n: with the first number denoting the frontmost position).
Any person in the queue can bribe the person directly in front of them to swap positions. If two people swap positions, they still wear the same sticker denoting their original place in line. One person can bribe at most two other persons.
That is to say, if n = 8 and Person 5 bribes Person 4, the queue will look like this:
1, 2, 3, 5, 4, 6, 7, 8.
Fascinated by this chaotic queue, you decide you must know the minimum number of bribes that took place to get the queue into its current state!
Note: Each Person X wears sticker X, meaning they were initially the Xth person in queue.
Input Format
The first line contains an integer, T, denoting the number of test cases.
Each test case is comprised of two lines; the first line has n (an integer indicating the number of people in the queue), and the second line has n space-separated integers describing the final state of the queue.
Constraints
$$ 1 leq T leq 10 $$
$$ 1 leq n leq 10^5 $$
Subtasks
For 60% score $$1 leq n leq 10^3$$
For 100% score $$1 leq n leq 10^5$$
Output Format
Print an integer denoting the minimum number of bribes needed to get the queue into its final state; print Too chaotic if the state is invalid (requires Person X to bribe more than 2 people).
Sample Input
2
5
2 1 5 3 4
5
2 5 1 3 4
Sample Output
3
Too chaotic
Any help/advice/suggestion is welcome.
#!/bin/python3
import sys
inp0 = sys.stdin.read()
inp1 = inp0.split('n')
T = int(inp1[0])
del inp1[0]
# Swaps the i'th and (i-1)'th elements and returns the list
def swap(intermediate_q,i):
intermediate_q[i],intermediate_q[i-1] = intermediate_q[i-1],intermediate_q[i]
return(intermediate_q)
# Increment bribes and total_bribes
def compute_bribes(bribes,total_bribes):
if(bribes+1==3):
return (-1,-1)
return (bribes+1,total_bribes+1)
for i in range(0,(2*T)-1,2):
n = int(inp1[i])
final_q = list(int(i) for i in inp1[i+1].split(' '))
initial_q = list(i+1 for i in range(n))
total_bribes,chaos_flag=0,0
for i in range(n):
bribes = 0
# If the position in the initial/transition queue is not equal to the position in the final queue
# final_q is the input against which swaps are made in my initial_q.
if(final_q.index(initial_q[i]) != initial_q.index(initial_q[i])):
index_in_initial_q = initial_q.index(final_q[i])
index_in_final_q = final_q.index(final_q[i])
while(index_in_initial_q != index_in_final_q):
initial_q = swap(initial_q,index_in_initial_q)
bribes,total_bribes = compute_bribes(bribes,total_bribes)
if(bribes == -1):
print("Too chaotic")
chaos_flag=1
break
index_in_initial_q-=1
if(chaos_flag == 1):
break
if (chaos_flag == 1):
continue
print(total_bribes)
python performance programming-challenge python-3.x time-limit-exceeded
$endgroup$
My code given below will produce the correct output but it tends to take way too much time with larger data thus timing out for some of the test cases. I've linked to the problem description here:
It's New Year's Day and everyone's in line for the Wonderland rollercoaster ride!
There are n people queued up, and each person wears a sticker indicating their initial position in the queue (i.e. 1, 2, ..., n - 1, n: with the first number denoting the frontmost position).
Any person in the queue can bribe the person directly in front of them to swap positions. If two people swap positions, they still wear the same sticker denoting their original place in line. One person can bribe at most two other persons.
That is to say, if n = 8 and Person 5 bribes Person 4, the queue will look like this:
1, 2, 3, 5, 4, 6, 7, 8.
Fascinated by this chaotic queue, you decide you must know the minimum number of bribes that took place to get the queue into its current state!
Note: Each Person X wears sticker X, meaning they were initially the Xth person in queue.
Input Format
The first line contains an integer, T, denoting the number of test cases.
Each test case is comprised of two lines; the first line has n (an integer indicating the number of people in the queue), and the second line has n space-separated integers describing the final state of the queue.
Constraints
$$ 1 leq T leq 10 $$
$$ 1 leq n leq 10^5 $$
Subtasks
For 60% score $$1 leq n leq 10^3$$
For 100% score $$1 leq n leq 10^5$$
Output Format
Print an integer denoting the minimum number of bribes needed to get the queue into its final state; print Too chaotic if the state is invalid (requires Person X to bribe more than 2 people).
Sample Input
2
5
2 1 5 3 4
5
2 5 1 3 4
Sample Output
3
Too chaotic
Any help/advice/suggestion is welcome.
#!/bin/python3
import sys
inp0 = sys.stdin.read()
inp1 = inp0.split('n')
T = int(inp1[0])
del inp1[0]
# Swaps the i'th and (i-1)'th elements and returns the list
def swap(intermediate_q,i):
intermediate_q[i],intermediate_q[i-1] = intermediate_q[i-1],intermediate_q[i]
return(intermediate_q)
# Increment bribes and total_bribes
def compute_bribes(bribes,total_bribes):
if(bribes+1==3):
return (-1,-1)
return (bribes+1,total_bribes+1)
for i in range(0,(2*T)-1,2):
n = int(inp1[i])
final_q = list(int(i) for i in inp1[i+1].split(' '))
initial_q = list(i+1 for i in range(n))
total_bribes,chaos_flag=0,0
for i in range(n):
bribes = 0
# If the position in the initial/transition queue is not equal to the position in the final queue
# final_q is the input against which swaps are made in my initial_q.
if(final_q.index(initial_q[i]) != initial_q.index(initial_q[i])):
index_in_initial_q = initial_q.index(final_q[i])
index_in_final_q = final_q.index(final_q[i])
while(index_in_initial_q != index_in_final_q):
initial_q = swap(initial_q,index_in_initial_q)
bribes,total_bribes = compute_bribes(bribes,total_bribes)
if(bribes == -1):
print("Too chaotic")
chaos_flag=1
break
index_in_initial_q-=1
if(chaos_flag == 1):
break
if (chaos_flag == 1):
continue
print(total_bribes)
python performance programming-challenge python-3.x time-limit-exceeded
python performance programming-challenge python-3.x time-limit-exceeded
edited Jul 29 '16 at 14:22
Pimgd
21.2k556142
21.2k556142
asked Jul 10 '16 at 11:50
Dhiwakar RavikumarDhiwakar Ravikumar
10817
10817
$begingroup$
There's a good chance you need a different algorithm. Possible approach: geeksforgeeks.org/counting-inversions
$endgroup$
– Robert Harvey
Jul 10 '16 at 15:20
add a comment |
$begingroup$
There's a good chance you need a different algorithm. Possible approach: geeksforgeeks.org/counting-inversions
$endgroup$
– Robert Harvey
Jul 10 '16 at 15:20
$begingroup$
There's a good chance you need a different algorithm. Possible approach: geeksforgeeks.org/counting-inversions
$endgroup$
– Robert Harvey
Jul 10 '16 at 15:20
$begingroup$
There's a good chance you need a different algorithm. Possible approach: geeksforgeeks.org/counting-inversions
$endgroup$
– Robert Harvey
Jul 10 '16 at 15:20
add a comment |
2 Answers
2
active
oldest
votes
$begingroup$
Code organisation
You've tried to split your logic into smallish functions which is a good idea but you could go further. You should try to write a function that handles the input/output part and a function which takes a well defined input (with the most relevant data types as an argument) and computes whatever needs to be computed before returning it (such a function should not do any input parsing or print anything except for debug purposes).
In you case, the most logical input such a function would take is the queue. The corresponding data type would be a list of int and the return type would be an integer (or None).
If you do so, you have smaller independant logical parts which are easier to understand, to maintain and to tests. Among other things, you can write unit tests based on the examples provided to ensure the computation works well.
In you case, moving the different pieces of logic around, you get something like:
#!/bin/python3
import sys
# Swaps the i'th and (i-1)'th elements and returns the list
def swap(intermediate_q,i):
intermediate_q[i],intermediate_q[i-1] = intermediate_q[i-1],intermediate_q[i]
return(intermediate_q)
# Increment bribes and total_bribes
def compute_bribes(bribes,total_bribes):
if(bribes+1==3):
return (-1,-1)
return (bribes+1,total_bribes+1)
def get_number_brides(queue):
"""Take a queue (list of int) as a parameter and return the number of brides or None."""
n = len(queue)
initial_q = list(i+1 for i in range(n))
total_bribes = 0
for i in range(n):
bribes = 0
# If the position in the initial/transition queue is not equal to the position in the final queue
# queue is the input against which swaps are made in my initial_q.
if(queue.index(initial_q[i]) != initial_q.index(initial_q[i])):
index_in_initial_q = initial_q.index(queue[i])
index_in_queue = queue.index(queue[i])
while(index_in_initial_q != index_in_queue):
initial_q = swap(initial_q,index_in_initial_q)
bribes,total_bribes = compute_bribes(bribes,total_bribes)
if(bribes == -1):
return None
index_in_initial_q-=1
return total_bribes
def test_stdio():
inp0 = sys.stdin.read()
inp1 = inp0.split('n')
T = int(inp1[0])
del inp1[0]
print("inp1", inp1)
for i, val in enumerate(inp1):
if i % 2 == 1:
ret = get_number_brides([int(v) for v in val.split(' ')])
print('Too chaotic' if ret is None else ret)
def unit_tests():
assert get_number_brides([2, 1, 5, 3, 4]) == 3
assert get_number_brides([2, 5, 1, 3, 4]) is None
if __name__ == "__main__":
unit_tests()
# test_stdio()
Among the nice benefits, because the function is now used for a single test case, I can return directly from the most relevant place and this is no need for a chaos_flag anymore (if you were to keep such a flag, it'd be a good idea to use the Boolean type).
Style
Python has a Style Guide called PEP 8 which is definitly worth having a look at and following. In your case, the spacing (both vertical and horizontal) is not quite perfect and so is the usage of useless parenthesis.
Fixing this, you get:
#!/bin/python3
import sys
# Swaps the i'th and (i-1)'th elements and returns the list
def swap(intermediate_q, i):
intermediate_q[i], intermediate_q[i-1] = intermediate_q[i-1], intermediate_q[i]
return(intermediate_q)
# Increment bribes and total_bribes
def compute_bribes(bribes, total_bribes):
if bribes + 1 == 3:
return (-1, -1)
return (bribes + 1, total_bribes + 1)
def get_number_brides(queue):
"""Take a queue (list of int) as a parameter and return the number of brides or None."""
n = len(queue)
initial_q = list(i + 1 for i in range(n))
total_bribes = 0
chaos_flag = False
for i in range(n):
bribes = 0
# If the position in the initial/transition queue is not equal to the position in the final queue
# queue is the input against which swaps are made in my initial_q.
if queue.index(initial_q[i]) != initial_q.index(initial_q[i]):
index_in_initial_q = initial_q.index(queue[i])
index_in_queue = queue.index(queue[i])
while index_in_initial_q != index_in_queue:
initial_q = swap(initial_q, index_in_initial_q)
bribes, total_bribes = compute_bribes(bribes, total_bribes)
if bribes == -1:
return None
index_in_initial_q -= 1
return total_bribes
def test_stdio():
inp0 = sys.stdin.read()
inp1 = inp0.split('n')
T = int(inp1[0])
del inp1[0]
print("inp1", inp1)
for i, val in enumerate(inp1):
if i % 2 == 1:
ret = get_number_brides([int(v) for v in val.split(' ')])
print('Too chaotic' if ret is None else ret)
def unit_tests():
assert get_number_brides([2, 1, 5, 3, 4]) == 3
assert get_number_brides([2, 5, 1, 3, 4]) is None
if __name__ == "__main__":
unit_tests()
# test_stdio()
I have no time to go any further but I hope this will help you, another reviewer and future me to take over.
$endgroup$
add a comment |
$begingroup$
Python3
def minimumBribes(q):
bribes = 0
for i in range(len(q)-1,-1,-1):
if q[i] - (i + 1) > 2:
print('Too chaotic')
return
for j in range(max(0, q[i] - 2),i):
if q[j] > q[i]:
bribes+=1
print(bribes)
New contributor
Itxel Zavala is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
$endgroup$
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "196"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f134435%2fhackerrank-new-year-chaos%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
Code organisation
You've tried to split your logic into smallish functions which is a good idea but you could go further. You should try to write a function that handles the input/output part and a function which takes a well defined input (with the most relevant data types as an argument) and computes whatever needs to be computed before returning it (such a function should not do any input parsing or print anything except for debug purposes).
In you case, the most logical input such a function would take is the queue. The corresponding data type would be a list of int and the return type would be an integer (or None).
If you do so, you have smaller independant logical parts which are easier to understand, to maintain and to tests. Among other things, you can write unit tests based on the examples provided to ensure the computation works well.
In you case, moving the different pieces of logic around, you get something like:
#!/bin/python3
import sys
# Swaps the i'th and (i-1)'th elements and returns the list
def swap(intermediate_q,i):
intermediate_q[i],intermediate_q[i-1] = intermediate_q[i-1],intermediate_q[i]
return(intermediate_q)
# Increment bribes and total_bribes
def compute_bribes(bribes,total_bribes):
if(bribes+1==3):
return (-1,-1)
return (bribes+1,total_bribes+1)
def get_number_brides(queue):
"""Take a queue (list of int) as a parameter and return the number of brides or None."""
n = len(queue)
initial_q = list(i+1 for i in range(n))
total_bribes = 0
for i in range(n):
bribes = 0
# If the position in the initial/transition queue is not equal to the position in the final queue
# queue is the input against which swaps are made in my initial_q.
if(queue.index(initial_q[i]) != initial_q.index(initial_q[i])):
index_in_initial_q = initial_q.index(queue[i])
index_in_queue = queue.index(queue[i])
while(index_in_initial_q != index_in_queue):
initial_q = swap(initial_q,index_in_initial_q)
bribes,total_bribes = compute_bribes(bribes,total_bribes)
if(bribes == -1):
return None
index_in_initial_q-=1
return total_bribes
def test_stdio():
inp0 = sys.stdin.read()
inp1 = inp0.split('n')
T = int(inp1[0])
del inp1[0]
print("inp1", inp1)
for i, val in enumerate(inp1):
if i % 2 == 1:
ret = get_number_brides([int(v) for v in val.split(' ')])
print('Too chaotic' if ret is None else ret)
def unit_tests():
assert get_number_brides([2, 1, 5, 3, 4]) == 3
assert get_number_brides([2, 5, 1, 3, 4]) is None
if __name__ == "__main__":
unit_tests()
# test_stdio()
Among the nice benefits, because the function is now used for a single test case, I can return directly from the most relevant place and this is no need for a chaos_flag anymore (if you were to keep such a flag, it'd be a good idea to use the Boolean type).
Style
Python has a Style Guide called PEP 8 which is definitly worth having a look at and following. In your case, the spacing (both vertical and horizontal) is not quite perfect and so is the usage of useless parenthesis.
Fixing this, you get:
#!/bin/python3
import sys
# Swaps the i'th and (i-1)'th elements and returns the list
def swap(intermediate_q, i):
intermediate_q[i], intermediate_q[i-1] = intermediate_q[i-1], intermediate_q[i]
return(intermediate_q)
# Increment bribes and total_bribes
def compute_bribes(bribes, total_bribes):
if bribes + 1 == 3:
return (-1, -1)
return (bribes + 1, total_bribes + 1)
def get_number_brides(queue):
"""Take a queue (list of int) as a parameter and return the number of brides or None."""
n = len(queue)
initial_q = list(i + 1 for i in range(n))
total_bribes = 0
chaos_flag = False
for i in range(n):
bribes = 0
# If the position in the initial/transition queue is not equal to the position in the final queue
# queue is the input against which swaps are made in my initial_q.
if queue.index(initial_q[i]) != initial_q.index(initial_q[i]):
index_in_initial_q = initial_q.index(queue[i])
index_in_queue = queue.index(queue[i])
while index_in_initial_q != index_in_queue:
initial_q = swap(initial_q, index_in_initial_q)
bribes, total_bribes = compute_bribes(bribes, total_bribes)
if bribes == -1:
return None
index_in_initial_q -= 1
return total_bribes
def test_stdio():
inp0 = sys.stdin.read()
inp1 = inp0.split('n')
T = int(inp1[0])
del inp1[0]
print("inp1", inp1)
for i, val in enumerate(inp1):
if i % 2 == 1:
ret = get_number_brides([int(v) for v in val.split(' ')])
print('Too chaotic' if ret is None else ret)
def unit_tests():
assert get_number_brides([2, 1, 5, 3, 4]) == 3
assert get_number_brides([2, 5, 1, 3, 4]) is None
if __name__ == "__main__":
unit_tests()
# test_stdio()
I have no time to go any further but I hope this will help you, another reviewer and future me to take over.
$endgroup$
add a comment |
$begingroup$
Code organisation
You've tried to split your logic into smallish functions which is a good idea but you could go further. You should try to write a function that handles the input/output part and a function which takes a well defined input (with the most relevant data types as an argument) and computes whatever needs to be computed before returning it (such a function should not do any input parsing or print anything except for debug purposes).
In you case, the most logical input such a function would take is the queue. The corresponding data type would be a list of int and the return type would be an integer (or None).
If you do so, you have smaller independant logical parts which are easier to understand, to maintain and to tests. Among other things, you can write unit tests based on the examples provided to ensure the computation works well.
In you case, moving the different pieces of logic around, you get something like:
#!/bin/python3
import sys
# Swaps the i'th and (i-1)'th elements and returns the list
def swap(intermediate_q,i):
intermediate_q[i],intermediate_q[i-1] = intermediate_q[i-1],intermediate_q[i]
return(intermediate_q)
# Increment bribes and total_bribes
def compute_bribes(bribes,total_bribes):
if(bribes+1==3):
return (-1,-1)
return (bribes+1,total_bribes+1)
def get_number_brides(queue):
"""Take a queue (list of int) as a parameter and return the number of brides or None."""
n = len(queue)
initial_q = list(i+1 for i in range(n))
total_bribes = 0
for i in range(n):
bribes = 0
# If the position in the initial/transition queue is not equal to the position in the final queue
# queue is the input against which swaps are made in my initial_q.
if(queue.index(initial_q[i]) != initial_q.index(initial_q[i])):
index_in_initial_q = initial_q.index(queue[i])
index_in_queue = queue.index(queue[i])
while(index_in_initial_q != index_in_queue):
initial_q = swap(initial_q,index_in_initial_q)
bribes,total_bribes = compute_bribes(bribes,total_bribes)
if(bribes == -1):
return None
index_in_initial_q-=1
return total_bribes
def test_stdio():
inp0 = sys.stdin.read()
inp1 = inp0.split('n')
T = int(inp1[0])
del inp1[0]
print("inp1", inp1)
for i, val in enumerate(inp1):
if i % 2 == 1:
ret = get_number_brides([int(v) for v in val.split(' ')])
print('Too chaotic' if ret is None else ret)
def unit_tests():
assert get_number_brides([2, 1, 5, 3, 4]) == 3
assert get_number_brides([2, 5, 1, 3, 4]) is None
if __name__ == "__main__":
unit_tests()
# test_stdio()
Among the nice benefits, because the function is now used for a single test case, I can return directly from the most relevant place and this is no need for a chaos_flag anymore (if you were to keep such a flag, it'd be a good idea to use the Boolean type).
Style
Python has a Style Guide called PEP 8 which is definitly worth having a look at and following. In your case, the spacing (both vertical and horizontal) is not quite perfect and so is the usage of useless parenthesis.
Fixing this, you get:
#!/bin/python3
import sys
# Swaps the i'th and (i-1)'th elements and returns the list
def swap(intermediate_q, i):
intermediate_q[i], intermediate_q[i-1] = intermediate_q[i-1], intermediate_q[i]
return(intermediate_q)
# Increment bribes and total_bribes
def compute_bribes(bribes, total_bribes):
if bribes + 1 == 3:
return (-1, -1)
return (bribes + 1, total_bribes + 1)
def get_number_brides(queue):
"""Take a queue (list of int) as a parameter and return the number of brides or None."""
n = len(queue)
initial_q = list(i + 1 for i in range(n))
total_bribes = 0
chaos_flag = False
for i in range(n):
bribes = 0
# If the position in the initial/transition queue is not equal to the position in the final queue
# queue is the input against which swaps are made in my initial_q.
if queue.index(initial_q[i]) != initial_q.index(initial_q[i]):
index_in_initial_q = initial_q.index(queue[i])
index_in_queue = queue.index(queue[i])
while index_in_initial_q != index_in_queue:
initial_q = swap(initial_q, index_in_initial_q)
bribes, total_bribes = compute_bribes(bribes, total_bribes)
if bribes == -1:
return None
index_in_initial_q -= 1
return total_bribes
def test_stdio():
inp0 = sys.stdin.read()
inp1 = inp0.split('n')
T = int(inp1[0])
del inp1[0]
print("inp1", inp1)
for i, val in enumerate(inp1):
if i % 2 == 1:
ret = get_number_brides([int(v) for v in val.split(' ')])
print('Too chaotic' if ret is None else ret)
def unit_tests():
assert get_number_brides([2, 1, 5, 3, 4]) == 3
assert get_number_brides([2, 5, 1, 3, 4]) is None
if __name__ == "__main__":
unit_tests()
# test_stdio()
I have no time to go any further but I hope this will help you, another reviewer and future me to take over.
$endgroup$
add a comment |
$begingroup$
Code organisation
You've tried to split your logic into smallish functions which is a good idea but you could go further. You should try to write a function that handles the input/output part and a function which takes a well defined input (with the most relevant data types as an argument) and computes whatever needs to be computed before returning it (such a function should not do any input parsing or print anything except for debug purposes).
In you case, the most logical input such a function would take is the queue. The corresponding data type would be a list of int and the return type would be an integer (or None).
If you do so, you have smaller independant logical parts which are easier to understand, to maintain and to tests. Among other things, you can write unit tests based on the examples provided to ensure the computation works well.
In you case, moving the different pieces of logic around, you get something like:
#!/bin/python3
import sys
# Swaps the i'th and (i-1)'th elements and returns the list
def swap(intermediate_q,i):
intermediate_q[i],intermediate_q[i-1] = intermediate_q[i-1],intermediate_q[i]
return(intermediate_q)
# Increment bribes and total_bribes
def compute_bribes(bribes,total_bribes):
if(bribes+1==3):
return (-1,-1)
return (bribes+1,total_bribes+1)
def get_number_brides(queue):
"""Take a queue (list of int) as a parameter and return the number of brides or None."""
n = len(queue)
initial_q = list(i+1 for i in range(n))
total_bribes = 0
for i in range(n):
bribes = 0
# If the position in the initial/transition queue is not equal to the position in the final queue
# queue is the input against which swaps are made in my initial_q.
if(queue.index(initial_q[i]) != initial_q.index(initial_q[i])):
index_in_initial_q = initial_q.index(queue[i])
index_in_queue = queue.index(queue[i])
while(index_in_initial_q != index_in_queue):
initial_q = swap(initial_q,index_in_initial_q)
bribes,total_bribes = compute_bribes(bribes,total_bribes)
if(bribes == -1):
return None
index_in_initial_q-=1
return total_bribes
def test_stdio():
inp0 = sys.stdin.read()
inp1 = inp0.split('n')
T = int(inp1[0])
del inp1[0]
print("inp1", inp1)
for i, val in enumerate(inp1):
if i % 2 == 1:
ret = get_number_brides([int(v) for v in val.split(' ')])
print('Too chaotic' if ret is None else ret)
def unit_tests():
assert get_number_brides([2, 1, 5, 3, 4]) == 3
assert get_number_brides([2, 5, 1, 3, 4]) is None
if __name__ == "__main__":
unit_tests()
# test_stdio()
Among the nice benefits, because the function is now used for a single test case, I can return directly from the most relevant place and this is no need for a chaos_flag anymore (if you were to keep such a flag, it'd be a good idea to use the Boolean type).
Style
Python has a Style Guide called PEP 8 which is definitly worth having a look at and following. In your case, the spacing (both vertical and horizontal) is not quite perfect and so is the usage of useless parenthesis.
Fixing this, you get:
#!/bin/python3
import sys
# Swaps the i'th and (i-1)'th elements and returns the list
def swap(intermediate_q, i):
intermediate_q[i], intermediate_q[i-1] = intermediate_q[i-1], intermediate_q[i]
return(intermediate_q)
# Increment bribes and total_bribes
def compute_bribes(bribes, total_bribes):
if bribes + 1 == 3:
return (-1, -1)
return (bribes + 1, total_bribes + 1)
def get_number_brides(queue):
"""Take a queue (list of int) as a parameter and return the number of brides or None."""
n = len(queue)
initial_q = list(i + 1 for i in range(n))
total_bribes = 0
chaos_flag = False
for i in range(n):
bribes = 0
# If the position in the initial/transition queue is not equal to the position in the final queue
# queue is the input against which swaps are made in my initial_q.
if queue.index(initial_q[i]) != initial_q.index(initial_q[i]):
index_in_initial_q = initial_q.index(queue[i])
index_in_queue = queue.index(queue[i])
while index_in_initial_q != index_in_queue:
initial_q = swap(initial_q, index_in_initial_q)
bribes, total_bribes = compute_bribes(bribes, total_bribes)
if bribes == -1:
return None
index_in_initial_q -= 1
return total_bribes
def test_stdio():
inp0 = sys.stdin.read()
inp1 = inp0.split('n')
T = int(inp1[0])
del inp1[0]
print("inp1", inp1)
for i, val in enumerate(inp1):
if i % 2 == 1:
ret = get_number_brides([int(v) for v in val.split(' ')])
print('Too chaotic' if ret is None else ret)
def unit_tests():
assert get_number_brides([2, 1, 5, 3, 4]) == 3
assert get_number_brides([2, 5, 1, 3, 4]) is None
if __name__ == "__main__":
unit_tests()
# test_stdio()
I have no time to go any further but I hope this will help you, another reviewer and future me to take over.
$endgroup$
Code organisation
You've tried to split your logic into smallish functions which is a good idea but you could go further. You should try to write a function that handles the input/output part and a function which takes a well defined input (with the most relevant data types as an argument) and computes whatever needs to be computed before returning it (such a function should not do any input parsing or print anything except for debug purposes).
In you case, the most logical input such a function would take is the queue. The corresponding data type would be a list of int and the return type would be an integer (or None).
If you do so, you have smaller independant logical parts which are easier to understand, to maintain and to tests. Among other things, you can write unit tests based on the examples provided to ensure the computation works well.
In you case, moving the different pieces of logic around, you get something like:
#!/bin/python3
import sys
# Swaps the i'th and (i-1)'th elements and returns the list
def swap(intermediate_q,i):
intermediate_q[i],intermediate_q[i-1] = intermediate_q[i-1],intermediate_q[i]
return(intermediate_q)
# Increment bribes and total_bribes
def compute_bribes(bribes,total_bribes):
if(bribes+1==3):
return (-1,-1)
return (bribes+1,total_bribes+1)
def get_number_brides(queue):
"""Take a queue (list of int) as a parameter and return the number of brides or None."""
n = len(queue)
initial_q = list(i+1 for i in range(n))
total_bribes = 0
for i in range(n):
bribes = 0
# If the position in the initial/transition queue is not equal to the position in the final queue
# queue is the input against which swaps are made in my initial_q.
if(queue.index(initial_q[i]) != initial_q.index(initial_q[i])):
index_in_initial_q = initial_q.index(queue[i])
index_in_queue = queue.index(queue[i])
while(index_in_initial_q != index_in_queue):
initial_q = swap(initial_q,index_in_initial_q)
bribes,total_bribes = compute_bribes(bribes,total_bribes)
if(bribes == -1):
return None
index_in_initial_q-=1
return total_bribes
def test_stdio():
inp0 = sys.stdin.read()
inp1 = inp0.split('n')
T = int(inp1[0])
del inp1[0]
print("inp1", inp1)
for i, val in enumerate(inp1):
if i % 2 == 1:
ret = get_number_brides([int(v) for v in val.split(' ')])
print('Too chaotic' if ret is None else ret)
def unit_tests():
assert get_number_brides([2, 1, 5, 3, 4]) == 3
assert get_number_brides([2, 5, 1, 3, 4]) is None
if __name__ == "__main__":
unit_tests()
# test_stdio()
Among the nice benefits, because the function is now used for a single test case, I can return directly from the most relevant place and this is no need for a chaos_flag anymore (if you were to keep such a flag, it'd be a good idea to use the Boolean type).
Style
Python has a Style Guide called PEP 8 which is definitly worth having a look at and following. In your case, the spacing (both vertical and horizontal) is not quite perfect and so is the usage of useless parenthesis.
Fixing this, you get:
#!/bin/python3
import sys
# Swaps the i'th and (i-1)'th elements and returns the list
def swap(intermediate_q, i):
intermediate_q[i], intermediate_q[i-1] = intermediate_q[i-1], intermediate_q[i]
return(intermediate_q)
# Increment bribes and total_bribes
def compute_bribes(bribes, total_bribes):
if bribes + 1 == 3:
return (-1, -1)
return (bribes + 1, total_bribes + 1)
def get_number_brides(queue):
"""Take a queue (list of int) as a parameter and return the number of brides or None."""
n = len(queue)
initial_q = list(i + 1 for i in range(n))
total_bribes = 0
chaos_flag = False
for i in range(n):
bribes = 0
# If the position in the initial/transition queue is not equal to the position in the final queue
# queue is the input against which swaps are made in my initial_q.
if queue.index(initial_q[i]) != initial_q.index(initial_q[i]):
index_in_initial_q = initial_q.index(queue[i])
index_in_queue = queue.index(queue[i])
while index_in_initial_q != index_in_queue:
initial_q = swap(initial_q, index_in_initial_q)
bribes, total_bribes = compute_bribes(bribes, total_bribes)
if bribes == -1:
return None
index_in_initial_q -= 1
return total_bribes
def test_stdio():
inp0 = sys.stdin.read()
inp1 = inp0.split('n')
T = int(inp1[0])
del inp1[0]
print("inp1", inp1)
for i, val in enumerate(inp1):
if i % 2 == 1:
ret = get_number_brides([int(v) for v in val.split(' ')])
print('Too chaotic' if ret is None else ret)
def unit_tests():
assert get_number_brides([2, 1, 5, 3, 4]) == 3
assert get_number_brides([2, 5, 1, 3, 4]) is None
if __name__ == "__main__":
unit_tests()
# test_stdio()
I have no time to go any further but I hope this will help you, another reviewer and future me to take over.
answered Aug 1 '16 at 9:56
JosayJosay
26k14087
26k14087
add a comment |
add a comment |
$begingroup$
Python3
def minimumBribes(q):
bribes = 0
for i in range(len(q)-1,-1,-1):
if q[i] - (i + 1) > 2:
print('Too chaotic')
return
for j in range(max(0, q[i] - 2),i):
if q[j] > q[i]:
bribes+=1
print(bribes)
New contributor
Itxel Zavala is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
$endgroup$
add a comment |
$begingroup$
Python3
def minimumBribes(q):
bribes = 0
for i in range(len(q)-1,-1,-1):
if q[i] - (i + 1) > 2:
print('Too chaotic')
return
for j in range(max(0, q[i] - 2),i):
if q[j] > q[i]:
bribes+=1
print(bribes)
New contributor
Itxel Zavala is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
$endgroup$
add a comment |
$begingroup$
Python3
def minimumBribes(q):
bribes = 0
for i in range(len(q)-1,-1,-1):
if q[i] - (i + 1) > 2:
print('Too chaotic')
return
for j in range(max(0, q[i] - 2),i):
if q[j] > q[i]:
bribes+=1
print(bribes)
New contributor
Itxel Zavala is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
$endgroup$
Python3
def minimumBribes(q):
bribes = 0
for i in range(len(q)-1,-1,-1):
if q[i] - (i + 1) > 2:
print('Too chaotic')
return
for j in range(max(0, q[i] - 2),i):
if q[j] > q[i]:
bribes+=1
print(bribes)
New contributor
Itxel Zavala is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Itxel Zavala is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
answered 8 mins ago
Itxel ZavalaItxel Zavala
1
1
New contributor
Itxel Zavala is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Itxel Zavala is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Itxel Zavala is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
add a comment |
Thanks for contributing an answer to Code Review Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f134435%2fhackerrank-new-year-chaos%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
$begingroup$
There's a good chance you need a different algorithm. Possible approach: geeksforgeeks.org/counting-inversions
$endgroup$
– Robert Harvey
Jul 10 '16 at 15:20