I am trying to write a program that reads numbers from a file and then calculates the sum of the squares of the numbers. This program is supposed to prompt the user for the name of the file and print the sum of the squares. It offers a hint that says use readlines but this hasn't helped me much. I have tried for hours to come up with a working code but can't get anything to work!!! I am about ready to pull my hair out!!!
Here is my code:.
If anyone could please tell me what I am doing wrong then it would be greatly appreciated. This is my first ever computer science class and any advice is welcome. Without knowing the structure of the file, I can at least tell you that part of your problem is that you are using the file handle as your "nums" variable which will not give you your content. In order to pull the data out of the file you will need to call. If you have one number per a line, you should be able to cast the contents of each list entry to an int to get a list of numbers.
If you have multiple numbers per a line, you need to use split on each list entry to extract each individual number. The num that receive the return of open fname, 'r' is a stream and not a text. Therefore you must use nums. See the function map of python, is very util to help you. The first thing to debug any code is by printing out at different points to check whether you are getting the desired output. The former will give you a list with each entry which is a string being one line of the file.
The second will give you the entire text converted to a single string. Also, if you're using Python 3. Learn more. Trying to write a python program that reads numbers from a file and computes the sum of the squares Ask Question.
Asked 6 years, 6 months ago. Active 6 years, 6 months ago.We use optional third-party analytics cookies to understand how you use GitHub. Learn more. You can always update your selection by clicking Cookie Preferences at the bottom of the page. For more information, see our Privacy Statement. We use essential cookies to perform essential website functions, e. We use analytics cookies to understand how you use our websites so we can make them better, e.
Skip to content. Instantly share code, notes, and snippets. Code Revisions 3. Embed What would you like to do? Embed Embed this gist in your website. Share Copy sharable link for this gist. Learn more about clone URLs. Download ZIP. Practice Question. Input: Your program should read lines from standard input. Each line will contain a positive integer.
Output: For each line of input, print to standard output the square of the number. Print out each result on a new line. Sign up for free to join this conversation on GitHub. Already have an account?
1.3 Conditionals and Loops
Sign in to comment. You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Accept Reject. Essential cookies We use essential cookies to perform essential website functions, e. Analytics cookies We use analytics cookies to understand how you use our websites so we can make them better, e. Save preferences. Programming Challenge Description:.
Write a program that squares an integer and prints the result. Your program should read lines from standard input.Most programs are more complicated because the sequence of statements and the number of times each is executed can vary. We use the term control flow to refer to statement sequencing in a program.Samsung bjnb4r emmc
If statements. Most computations require different actions for different inputs. The following code fragment uses an if statement to put the smaller of two int values in x and the larger of the two values in yby exchanging the values in the two variables if necessary. The table below summarizes some typical situations where you might need to use an if or if-else statement.
While loops. Many computations are inherently repetitive. The while loop enables us to execute a group of statements many times. This enables us to express lengthy computations without writing lots of code. The following code fragment computes the largest power of 2 that is less than or equal to a given positive integer n. For loops.
The for loop is an alternate Java construct that allows us even more flexibility when writing loops. For notation. Many loops follow the same basic scheme: initialize an index variable to some value and then use a while loop to test an exit condition involving the index variable, using the last statement in the while loop to modify the index variable. Java's for loop is a direct way to express such loops.Diagram based the ignition wiring diagram for evinrude 55hp
Compound assignment idioms. The scope of a variable is the part of the program that can refer to that variable by name. Generally the scope of a variable comprises the statements that follow the declaration in the same block as the declaration.
For this purpose, the code in the for loop header is considered to be in the same block as the for loop body. The ifwhileand for statements have the same status as assignment statements or any other statements in Java; that is, we can use them wherever a statement is called for.
In particular, we can use one or more of them in the body of another statement to make compound statements. To emphasize the nesting, we use indentation in the program code. It prints a pattern of asterisks where the i th row has an asterisk in each position corresponding to divisors of i the same holds true for the columns.
It uses several nested if-else statements to test from among a number of mutually exclusive possibilities.
Loop examples.We use optional third-party analytics cookies to understand how you use GitHub. Learn more. You can always update your selection by clicking Cookie Preferences at the bottom of the page.
For more information, see our Privacy Statement. We use essential cookies to perform essential website functions, e. We use analytics cookies to understand how you use our websites so we can make them better, e. Skip to content. Instantly share code, notes, and snippets. Code Revisions 1 Stars 1 Forks 2. Embed What would you like to do? Embed Embed this gist in your website. Share Copy sharable link for this gist.
Learn more about clone URLs. Download ZIP.70s tv shows uk
HireVue test question answer - Java. Input: Your program should read lines from standard input. Each line will contain a positive integer. Output: For each line of input, print to standard output the square of the number. Print out each result on a new line. This comment has been minimized. Sign in to view. Copy link Quote reply. Sign up for free to join this conversation on GitHub.
Write program in java to print the squares of first 25 integers without using any variables. I am not looking for any theory explaining concepts about java or functional programming. I just want to know if its possible in java, the way the author has described in the original context.
The original context was. Indeed, it has nothing more than three functions and one constant. Try writing the squares of integers in Java without using a variable.
For those who don't understand: It takes the stacks length squares it.
This works because I'm recursively calling malways increasing the stack by 1. Maybe this could be written even shorter. Now that you completely changed the question, my solution depends on whether you consider function arguments to be variables or not. I'd say it still stands. I should add that the Closure solution depends on the pre-availability of several functions in the standard library.
The same functions could be written in Java, leading to a nearly identical main program:. Sign up to join this community. The best answers are voted up and rise to the top. Write a program to print squares of integers up to 25 in java without using variables? Asked 7 years, 7 months ago. Active 4 years, 1 month ago. Viewed 10k times.
Furthermore, you got answers on programmers that are completely equivalent to what a pure functional language does under the hood. Active Oldest Votes. Justin Atmocreations Atmocreations 1 1 silver badge 3 3 bronze badges. The same functions could be written in Java, leading to a nearly identical main program: import java. AbstractList; import java. Not sure what else you are looking for. Maybe be some kind of hack or cheat or maybe something else. I never thought it was possible to write a java program without using main method.Check for a perfect square - Python Mathematics Project 1
Its possible actually. Take your pick.
C# Back to Basics – Input and Output
Code Golf Stack Exchange is a site for recreational programming competitions, not general programming questions. Challenges must have an objective scoring criterionand it is highly recommended to first post proposed challenges in the Sandbox. The Overflow Blog. The Overflow Bugs vs. How to put machine learning models into production.
Subscribe to RSS
Featured on Meta.In this article, we are going to learn how to solve simple problems by using linear structures. We are going to accept inputs, work with them and print the output to the console window. For the complete navigation of this series check out: C Back to Basics. If you want to download the source code for our examples, you can do that from here Input and Output Source Code.
Example 1: Create an application that prints out the sum of two integer values which user inputs in the console window. ToInt32 Console. ReadLine ; Console. WriteLine statement, we display the message on the console window and move to the next line. The Console.
ReadLine statement will read our input, but it is of type string and what we need is an int type. So, we need to convert it with the Convert. ToInt32 statement. Finally, we calculate the sum and print it out.
ReadKey statement is here just to keep our console window open. If we run our project by hitting F5, we will see the result with the name and last name, space-separated. Now we know how to manipulate inputs in our applications and how to display the result in the console window. Furthermore, we have used the data conversion from string to intto be able to sum the inputs from a user.
Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. Now I need to write another for loop that prints each number and its square on a new line, like this:.Miui gps fix
So far I have actually been able to print the squared values, but can't get them on separate lines by themselves. I also need to rid of the brackets around the values.
How to Calculate Square and Square Root in Java?
I need to get rid of the brackets and need to only include one squared value per line that corresponds with the integer to the left. How should I do this? I'm not sure if you actually want to track the squares or if that was just part of your effort to get them printed.
This allows you to have the current square ready to work with without having to reference it from the array. If you really did mean to reference the squared value from the array, you'd use a negative index to fetch from the end of the array:. Learn more.
Asked 3 years, 11 months ago. Active 6 months ago.
Viewed 6k times. HappyHands31 HappyHands31 3, 4 4 gold badges 32 32 silver badges 62 62 bronze badges. Why build a list? You don't need to collect the values in a new list. I don't know why I didn't think of that - yes it works as expected, thank you! Active Oldest Votes. Ouroborus Ouroborus 9, 23 23 silver badges 48 48 bronze badges. Using the negative index gives me for every squared result, but your first block of code does what I was looking for.
Except, Is there any way to print a result without the commas? HappyHands31 I apologize for that. I was still partially in a Python 2 state of mind. I've updated the code to Python 3, fixed a bug in the second chunk, and added some online demos.
- How to take apart a proform treadmill
- Msi rgb splitter
- 2020 07 iht the office season 3 episode 15 dailymotion
- Urdf viewer online
- Can you craft on corrupted items poe
- Cd4066 application
- Tywe3s tasmota
- Departed from the first mile sorting center
- Paola vidulli. diseño de bibliotecas: guia para planificar y proyectar
- Fcps part 1 anesthesia syllabus
- Re zero light novel summary
- 1995 mazda mx5 miata electrical wiring diagram original diagram
- Dokta sule na tiba
- Makan tradisional brunei
- Sonntag, 17. september 2017
- Manhwa xyz
- Radio signal mode detection software
- Struggling as a mom
- Ford sync 3 waze waiting for device
- Ara bhojpur ka news