Lua Text and function print in the same line [closed] - lua

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 6 years ago.
Improve this question
Can any body help me ?
function sum(_g, _h)
local num = (_g * _h) / 2
return num
end
print("The result is")(sum(10, 6))
Why this isn't working ?

The function print takes one or more strings as arguments.
When the strings are entered as different arguments, it outputs them separated by a tab
The result is 20
To get this output, just imagine you store the return from sum in a variable
res = sum(10, 6)
And then call print entering your string and the result just like you enter 10 and 6 in your function sum:
print("The result is ", res)
This also traduces to
print("The result is ", sum(10, 6))
Without needing to store the result anywhere.
Anyway if you target an output which looks like
The result is 20
you must enter only one string as the argument for print
..
is the operator which lets you concatenate two string in one string, so that "hello".." world" results in "hello world".
Now just combine the two strings "The result is " and 20 (which actually is a number, but it gets automatically converted to a string) with the .. operator, as in
res = sum(10, 6)
mystring = "The result is "
print(mystring..res)
Or, more shortly
print("The result is "..sum(10, 6))

Related

Decimal value is not recognized by F# console [closed]

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 1 year ago.
Improve this question
I'm practicing F# for the first time and I thought I'd try to make a program that would calculate the areas of different kinds of shapes. However, the portion that finds the area of a circle is giving me a lot of trouble.enter code here
elif stringInput = "2" then
let PI = 3.14156
printfn "What is the circle's radius: "
let radiusString = System.Console.ReadLine()
let radiusInt = radiusString |> float
let cirlceArea = (radiusInt * radiusInt) * PI
printfn "The area of the circle is : %d" cirlceArea
I'm sure it has something to do with the radiusString |> float part of the code, but nothing I've tried works and I've had no luck in finding any examples that can help. What can I do?
Ok I just found out the problem was that I was using %d instead of %f

Why is this haskell function giving me a parse error? [closed]

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 2 years ago.
Improve this question
I am trying to say if the desired location in the field is 1 return true otherwise return false. Why is this code not working?
fireShot :: Coordinate -> Field -> Bool
fireShot coord Shipfield
| nth ( fst(coord)((nth snd(coord)) ShipField) == 1 = True
| otherwise = False
The brackets in the guard are not balanced, you open five brackets, and you close four brackets. Furthermore variables start with a lowercase, so it should (probably) be shipfield, not Shipfield.
I think it might be better to use pattern matching to obtain the first and second coordinate, since this will make the code more clean. You furthermore do not need guards to return True and False. You can replace the function with:
fireShot :: Coordinate -> Field -> Bool
fireShot (x,y) shipfield = nth x (nth y shipfield) == 1

Attempt to perform arithmetic on field 'duration' (a string value) [duplicate]

This question already has an answer here:
Lua: attempt to perform arithmetic on a string value
(1 answer)
Closed 5 years ago.
Here is my error message:
Panel: [name:DItemSlot][class:Panel][138,69,64,64]
[ERROR] addons/pointshop2/lua/ps2/client/notifications/cl_knotificationpanelmanager.lua:91: attempt to perform arithmetic on field 'duration' (a string value)
1. unknown - addons/pointshop2/lua/ps2/client/notifications/cl_knotificationpanelmanager.lua:91
And here is my code:
if not self.panelSlidingIn and #self.notificationsWaiting > 0 then
self.panelSlidingIn = table.remove( self.notificationsWaiting, 1 ) --
Dequeue
self.panelSlidingIn:SetParent( self )
self.panelSlidingIn:SetVisible( true )
self.panelSlidingIn.slideOutStart = CurTime( ) +
self.panelSlidingIn.duration + self.slideInDuration
self.slidingStarted = CurTime( )
table.insert( self.notifications, self.panelSlidingIn )
surface.PlaySound( self.panelSlidingIn.sound or
"kreport/misc_menu_4.wav" )
end
I don't know what is happening, and I can't seem to fix it either.
When you use + operator on string Lua tries to convert them to numbers. If it couldn't it gives you such errors. If you want to concatinate strings use operator .. instead. If you want to make arithmetics, make sure strings have convertable values.
Similar question: here.

Why does use of := with fmt.Scanf in Go always return 1? [closed]

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 5 years ago.
Improve this question
Here is the code:
var n int
a, _ := fmt.Scanf("%d",&n)
Then a == 1, n has changed its value by input. Why does use of := with fmt.Scanf in Go always return 1?
fmt.Scanf() returns the number of successfully scanned items:
Scanf scans text read from standard input, storing successive space-separated values into successive arguments as determined by the format. It returns the number of items successfully scanned. If that is less than the number of arguments, err will report why.
So if your input is a valid integer number fitting into an int, fmt.Scanf() will succeed to parse it and store it in n, and so it will return 1.
Should you input an invalid number (e.g. the string value "a"), scanning would not succeed, so 0 would be returned along with a non-nil error, like in this example:
var n int
a, err := fmt.Sscanf("a", "%d", &n)
fmt.Println(a, err)
Which outputs (try it on the Go Playground):
0 expected integer

finding the phone number whose digits sum to the largest number in Ruby [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
This is my first time using Ruby and i need to take an array with three 10 digits phone numbers and have the program give me the phone number whose sum is the greatest.
my_array = (['123-456-7777', '963-481-7945', '111-222-3333'])
=> '963-481-7945'
I have no idea where to start.
Please help!
I am currently doing the codeacademy course on Ruby. But so far I don't know enough to get through this problem.
I thought about starting with
my_array = (['123-456-7777', '963-481-7945', '111-222-3333'])
my_array[2]
which would give me the desired answer of
=> "963-481-7945"
but i know that's not the way to go.
I was thinking so performing a sum for each value and then setting it equal to the set of number I want to display but I'm not sure how to set that up.
The solution would something like this:
my_array = (['123-456-7777', '963-481-7945', '111-222-3333'])
highest_sum = 0
highest_phone_number = 0
my_array.each do |phone_number|
sum = phone_number.gsub('-', '').split('').map(&:to_i).reduce(&:+)
if sum > highest_sum
highest_sum = sum
highest_phone_number = phone_number
end
end
puts highest_phone_number
#=> 963-481-7945
You look through each string in the array of phone number strings (my_array) and get the sum of each phone number string. You do this by replacing the dashes ('-') with nothing (''), splitting the phone number string into digits, mapping each digit to an integer, and then summing the digits. Once you get the sum of the phone number, check to see if it is larger than the current highest_sum. If so, set the highest_sum to that sum and the highest_phone_number to the current phone number. After you've looped though all the phone number strings, just output the highest_phone_number.
Here is my solution:
my_array = (['123-456-7777', '963-481-7945', '111-222-3333', '222-111-3333'])
sum_arr = []
sum_hash = {}
my_array.each do |s|
sum = s.gsub("-","").split(//).inject(0) {|z, x| z + x.to_i}
puts sum
sum_hash[sum] = sum_hash[sum] ? sum_hash[sum] << s : [s]
puts sum_hash.inspect
end
puts sum_hash.max_by { |k,v| k }
Part of my solution come from: http://rosettacode.org/wiki/Sum_digits_of_an_integer
Note: The question changed between during I wrote my answer :(

Resources