The discussion centers on creating a Python function, `typing_speed`, that calculates typing speed in words per minute based on the number of words typed and the time interval in seconds. The initial code provided attempts to achieve this but is critiqued for using unclear variable names and potentially incorrect division behavior. It is pointed out that Python handles division differently than some other languages, with both integer and floating-point division. The main issue identified is that the division operation could yield an incorrect result if both operands are integers, leading to integer division. To correct this, it's suggested that at least one operand should be cast to a float before performing the division to ensure accurate calculations. The revised function uses clearer variable names and ensures proper type handling to return the correct typing speed.
#1
zeion
455
1
Homework Statement
Write the definition of a function typing_speed , that receives two parameters. The first is the number of words that a person has typed (an int greater than or equal to zero) in a particular time interval. The second is the length of the time interval in seconds (an int greater than zero). The function returns the typing speed of that person in words per minute (a float ).
When you include code in a post, use [ code] and [ /code] tags (without the extra space). Doing so preserves your indentation, which is especially important in python.
zeion said:
Homework Statement
Write the definition of a function typing_speed , that receives two parameters. The first is the number of words that a person has typed (an int greater than or equal to zero) in a particular time interval. The second is the length of the time interval in seconds (an int greater than zero). The function returns the typing speed of that person in words per minute (a float ).
Homework Equations
The Attempt at a Solution
Code:
def typing_speed(a, b):
return float((a/b)*60)
Is that right?
I don't think so.
First off - your variable names don't give any clue as to what they are to be used for. It's usually a good idea to use names that help you and other readers understand how the variable will be used.
I'm not very adept at python, but I suspect that it defines two types of division the same way that C, C++, Java, and other programming languages do. One type of division is integer division, in which the dividend and divisor are both one of the integral types, and in which the quotient is also an integer. With this type of division, 6/2 == 3 (which you would expect), but 6/4 == 1 (which you might not expect.
The other type of division is floating point division, which behaves as you would expect.
Let's suppose that someone types 170 words in 2 minutes (120 seconds). Your function will evaluate 170/120 * 60. It first evaluates 170/120 as 1, with no fractional part. It then multiplies 1 by 60 to get 60, and then casts that result to a float value, 60.0. This is not the right answer, since the correct result is actually 85.0 words per minute.
To fix your code you need to cast one or both operands of the division as a float or double. You are using a cast to convert a/b * 60 to a float, but you are doing it too late. The cast operation needs to happen before the division takes place, not after.
Dear Peeps
I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are
Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM?
I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...