Click here to Skip to main content
15,881,840 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
<pre>
Scanner sc = new Scanner(System.in);

long sum = 0;

int N = sc.nextInt();

for (int i = 0; i < N; i++) {

final long x = sc.nextLong(); // read input

String str = Long.toString((long) Math.pow(1 << 1, x));

str = str.length() > 2 ? str.substring(str.length() - 2) : str;

sum += Integer.parseInt(str);

}

I want to write a equivalent python program
Can anyone help me understand the logic of this Java Code ?


Input
4 8 6 7 4
Output
64

Example 2

Input
3
1 2 3

Output
14

What I have tried:

n = int(input())
nums = list(map(int,input().split(' '))
while n>2:
#LOGIC
Posted
Updated 9-Aug-18 23:30pm

So you have found some code that does what your homework asks, and you want it explained to you so you can submit it as your own work?

We do not do your homework: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.

Instead of spending your time trying to find something that you can hand in, try reading the question carefully, and writing your own code. You will learn a whole load more that way!
Try it yourself, you may find it is not as difficult as you think!

If you meet a specific problem, then please ask about that and we will do our best to help. But we aren't going to do it all for you!
 
Share this answer
 
Comments
Member 13944678 10-Aug-18 4:47am    
Sorry, but i'm not asking anyone to write the whole code for me.
I just want to understand what is happening in this code written in Java(I don't know Java)
OriginalGriff 10-Aug-18 4:54am    
And why are you doing that?
Member 13944678 10-Aug-18 5:10am    
I found a code in Java that i want to write in python using the same logic.
The problem is that i don't understand Java.
If i know what the coding is taking input , what operation it performs , output.
I can easily write it for python3.
Help me out !!
Nelek 10-Aug-18 8:27am    
If you don't understand what the code does, how do know it doesn't what you need to
You should get together with your colleague who posted the question at https://www.codeproject.com/Questions/1255912/How-do-I-solve-this-I-am-unable-to-solve[^]. I am sure between the two of you you can work it out.
 
Share this answer
 
Quote:
Can someone explain the LOGIC of this code ?

use a debugger and watch the code as it execute, you will see by yourself what it does.

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your cpde is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.
Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
Debugging C# Code in Visual Studio - YouTube[^]
The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900