Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
The goal here is to try and find the average of the three highest inputted grades, when four numbers are put in. I think I've come close but I'm running into a syntax error and I can't seem to find what is causing it.
I've got almost no experience with python so excuse the sloppy code but this is what I've gotten so far.
def highestThreeAvg(grade1 : float, grade2 : float, grade3 : float, grade4 : float) -> float:
"""Given the grades, grade1, grade 2, grade3, grade4, returns the average
of the three highest grades"""
return (((grade1 + grade2 + grade3 + grade4) - lowestGrade) / 3
grade1 = float(input("Enter first grade: "))
grade2 = float(input("Enter second grade: "))
grade3 = float(input("Enter third grade: "))
grade4 = float(input("Enter fourth grade: "))
def lowest(grade1, grade2, grade3, grade4):
if (grade1 < grade2) and (grade1 < grade3) and (grade1 < grade4):
lowestGrade = grade1
elif (grade2 < grade1) and (grade2 < grade3) and (grade2 < grade4):
lowestGrade = grade2
elif (grade3 < grade1) and (grade3 <grade2) and (grade3 < grade4):
lowestGrade = grade3
else:
lowestGrade = grade4
print(lowestGrade)
lowest(grade1, grade2, grade3, grade4)
print(highestThreeAvg)
The syntax error that I'm running into is at the start of line 6
grade1 = float(input("Enter first grade: "))
^
SyntaxError: invalid syntax
If you see any other issues with my code or where I can improve it feel free to let me know.
Placing an additional close bracket, as shown below, at the end of line 4 should solve the syntax issue.
return (((grade1 + grade2 + grade3 + grade4) - lowestGrade) / 3)
It seems that you have 3 (, but only 2 ) in the previous line !
Related
Having an infinite sequence s = 1234567891011...
Let's find the number at the n position (n <= 10^18)
EX: n = 12 => 1; n = 15 => 2
import Foundation
func findNumber(n: Int) -> Character {
var i = 1
var z = ""
while i < n + 1 {
z.append(String(i))
i += 1
}
print(z)
return z[z.index(z.startIndex, offsetBy: n-1)]
}
print(findNumber(n: 12))
That's my code but when I find the number at 100.000th position, it returns an error, I thought I appended too many i to z string.
Can anyone help me, in swift language?
The problem we have here looks fairly straight forward. Take a list of all the number 1-infinity and concatenate them into a string. Then find the nth digit. Straight forward problem to understand. The issue that you are seeing though is that we do not have an infinite amount of memory nor time to be able to do this reasonably in a computer program. So we must find an alternative way around this that does not just add the numbers onto a string and then find the nth digit.
The first thing we can say is that we know what the entire list is. It will always be the same. So can we use any properties of this list to help us?
Let's call the input number n. This is the position of the digit that we want to find. Let's call the output digit d.
Well, first off, let's look at some examples.
We know all the single digit numbers are just in the same position as the number itself.
So, for n<10 ... d = n
What about for two digit numbers?
Well, we know that 10 starts at position 10. (Because there are 9 single digit numbers before it). 9 + 1 = 10
11 starts at position 12. Again, 9 single digits + one 2 digit number before it. 9 + 2 + 1 = 12
So how about, say... 25? Well that has 9 single digit numbers and 15 two digit numbers before it. So 25 starts at 9*1 + 15*2 + 1 = 40 (+ 1 as the sum gets us to the end of 24 not the start of 25).
So... 99 starts at? 9*1 + 89*2 + 1 = 188.
The we do the same for the three digit numbers...
100... 9*1 + 90*2 + 1 = 190
300... 9*1 + 90*2 + 199*3 + 1 = 787
1000...? 9*1 + 90*2 + 900*3 + 1 = 2890
OK... so now I'm seeing a pattern here that seems to need to know the number of digits in each number. Well... I can get the number of digits in a number by rounding up the log(base 10) of that number.
rounding up log base 10 of 5 = 1
rounding up log base 10 of 23 = 2
rounding up log base 10 of 99 = 2
rounding up log base 10 of 627 = 3
OK... so I think I need something like...
// in pseudo code
let lengthOfNumber = getLengthOfNumber(n)
var result = 0
for each i from 0 to lengthOfNumber - 1 {
result += 9 * 10^i * (i + 1) // this give 9*1 + 90*2 + 900*3 + ...
}
let remainder = n - 10^(lengthOfNumber - 1) // these have not been added in the loop above
result += remainder * lengthOfNumber
So, in the above pseudo code you can give it any number and it will return the position in the list that that number starts on.
This isn't the exact same as the problem you are trying to solve. And I don't want to solve it for you.
This is just a leg up on how I would go about solving it. Hopefully, this will give you some guidance on how you can take this further and solve the problem that you are trying to solve.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I need to figure out what number base makes the expression 32 + 12 = 28 true. It looks like the second number is signed, I've tried converting to decimal and using 10's complement but haven't gotten an answer that makes sense.
If this is a positional number system, then you can replace the numerals with an expression showing how their digits multiply by the meaning of their columns.
That is,
"32 + 12 = 28"
≣ (3*R^1 + 2*R^0) + (1*R^1 + 2*R^0) = (2*R^1 + 8*R^0)
≣ (3*R + 2*1) + (1*R + 2*1) = (2*R + 8*1)
≣ (3*R + 2) + (1*R + 2) = (2*R + 8)
≣ 4*R + 4 = 2*R + 8
≣ 2*R = 4
≣ R = 2
For some radix R.
You can solve this equation to get the value of R. If you attempt to solve it, you'll see that you get R = 2, which clearly can't be the case, since you have a digit 3 (a base-2 system would only have ... 2 digits. 0, and 1).
Thus, there is no radix R that would allows "32 + 12 = 28" to encode a valid equation.
On the face of it, it looks impossible. Adding positive integers in any base should result in a larger integer; if the second integer is negative, you end up with 20 = 28 which is clearly impossible. Considered modulo arithmetic? 32 mod 4 is 0, 12 mod 4 is 0, 28 mod 4 is 0, 0 + 0 = 0.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Can anyone help me out with this sequece.
I have tried multiple times but not getting solution. Some of the solution looks like as follows
1
11
21
1211
11121
But i need following solution. I want to print this sequece in console.
1
11
21
1211
3112
132112
311322
var last = [1, 3, 2, 1, 1, 2]
var next = [Int]()
func getNext() {
if last.count == 0 { return }
let first = last.first!
let firstCount = last.filter{ $0 == first }.count
next.append(firstCount)
next.append(first)
last.removeAll { $0 == first }
getNext()
}
getNext()
print(next)//[3, 1, 1, 3, 2, 2]
I've just started Scala at University and we're currently just doing the basics but I'm moved ahead and want to try making some basic programs to get ahead. I'm currently trying to make a basic dice role guessing program.
I want to add in a feature to replay the game, the counter is to tell the user how many attempts to guess. I know I can add it in somewhere with counter += 1 just not sure where in the program to put it.
Currently compiling does nothing until I input something and I'm unsure why, then when I input something I don't get any results from the if statements it just terminates the program.
import scala.io.StdIn.readInt
import scala.util.Random
object GuessTheNumber {
def main(args: Array[String]): Unit = {
var play = true
var counter = 0 //Allows counting input
var responseGuess = readInt()
val numRange = Random.nextInt(12)
println("Enter your guess of the result of 2, 6 sided dice.")
while(play) {
if (responseGuess == numRange)
println ("You got it right!")
else if (numRange < responseGuess)
("Your guess was too large by" + (responseGuess - numRange) + " the corrrect answer was " + numRange)
else if (numRange > responseGuess)
( "Your guess was too small by" + (numRange - responseGuess) + " the correct answer was " + numRange)
else if (responseGuess < 1)
("Your guess is too small enter 2-12")
else if (responseGuess > 12)
("You entered an invalid number, 2 dice can only equate to a maximum of 12")
}
}
}
A couple things:
You should ask for a guess (the println) before you go get it from the input
All of your else if blocks were missing println statements
You aren't using your counter
Your numRange is incorrect, right now it would return 0 to 11 inclusive
I made those changes, and moved the readInt() inside the while loop to give people multiple chances to guess, once they guess the answer then the loop exited.
var play = true
val numRange = 2 + Random.nextInt(11)
while(play) {
println("Enter your guess of the result of 2, 6 sided dice.")
var responseGuess = readInt()
if (responseGuess == numRange) {
println ("You got it right!")
play = false
}
else if (numRange < responseGuess)
println("Your guess was too large")
else if (numRange > responseGuess)
println( "Your guess was too small")
else if (responseGuess < 2)
println("Your guess is too small enter 2-12")
else if (responseGuess > 12)
println("You entered an invalid number, 2 dice can only equate to a maximum of 12")
}
You can build off of this to get the functionality you need.
You had a few basic errors, but, a really good first attempt. I have included most of the comments in context for clarity. Here is a quick summary of some of the things that needed minor corrections
You forgot to include println in every else clause
You didn't reset the value of play variable causing your program to be stuck in an infinite loop. The while loop kept executing without asking user for any input giving the illusion that nothing was printing
The counter needs to be updated after every attempt within the loop
import scala.io.StdIn.readInt
import scala.io.StdIn.readChar
import scala.util.Random
object GuessTheNumber {
def main(args: Array[String]): Unit = {
var play = true
var counter = 1 //Allows counting input
// Start a loop. As long as the user says he wants to continue, we keep entering the loop
while (play) {
// The game is slightly more clear in my humble opinion if you ask him/her this question after every attempt.
// Otherwise you just have a blinking cursor on screen, without prompting user for input
println("Enter your guess of the result of 2, 6 sided dice.")
// Read his current response
val responseGuess = readInt()
// If you don't have `1 +`, then, 0 could be a potential result
val numRange = 2 + Random.nextInt(11)
if (responseGuess == numRange)
println(s"You got it right! in $counter attempts")
// You were missing println in each of the following cases. Hence the lack of response from your program, since,
// it didn't know you wanted it to print that response
else if (numRange < responseGuess)
println("Your guess was too large by " + (responseGuess - numRange) + " the correct answer was " + numRange)
else if (numRange > responseGuess)
println( "Your guess was too small by " + (numRange - responseGuess) + " the correct answer was " + numRange)
else if (responseGuess < 1)
println("Your guess is too small enter 2-12")
else if (responseGuess > 12)
println("You entered an invalid number, 2 dice can only equate to a maximum of 12")
println("Do you want to continue? (Y/N)")
// Now you are reading a character instead of an integer. Basically a Y or an N
play = readChar() match { // Not sure if you have gotten this far in your course. This syntax is referred to as pattern matching.
// Its basically the same as using if / else
case 'Y' | 'y' => true // If he enters Y or y, we assume he wants to continue.
case _ => // For any other input we abort
println(s"You tried $counter times") // When he does decide to quit, lets tell him how many times he tried
false
}
// After every attempt increment counter within your loop
counter += 1
}
}
}
Hope this helps.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
Can someone help me understand why the following code causes the above error?
The code is for rotation of a 2D array by 90 degrees.
def rotate90(block: Array[Array[Int]]) = {
var size: Int = block.size
var i: Int = 0
var j: Int = size - 1
while (j >= 0) {
while (i <= j) {
val a = block[j][i]
val b = block[j - i][j]
block[j][i] = b
block[j - i][j] = a
i = i + 1
}
j = j - 1
}
return block
}
def rotate90(block: Array[Array[Int]]) = {
val copy: Array[Array[Int]] = Array.ofDim[Int](block.length, block(0).length)
for (w <- 0 until block(0).length;
h <- 0 until block.length) {
copy(h).update(w, block(block(0).length - 1 - w)(h))
}
copy
}
Above is scala solution for you problem. Maybe you better understand how arrays in scala works.