1

I'm learning Python from Brian Heinold's A Practical Introduction to Python Programming where exercise 24 in chapter 6 reads:

In calculus, the derivative of x4 is 4x3. The derivative of x5 is 5x4. The derivative of x6 is 6x5. This pattern continues. Write a program that asks the user for input like x^3 or x^25 and prints the derivative. For example, if the user enters x^3, the program should print out 3x^2.

I figured it out. Easy. However the trick is that should be solved without using int() since it has not been mentioned in the book so far. Could you please tell me how to do that?

Here is my solution:

original = input("Enter an x with a power: ")
part1 = original[2:]
part2 = original[0]
part3 = original[1]
part4 = str(int(original[2:])-1)
derivative = part1 + part2 + part3 + part4
print("The derivative is", derivative)
7
  • 1
    Hard to know what has been mentioned in the book so far. If there's no way to directly convert user input to an int, perhaps you are supposed to manipulate the digit strings (as characters) to build the answer. Commented Nov 12, 2022 at 19:33
  • @alexis manually implement addition and subtraction for string integer digits? Commented Nov 12, 2022 at 19:34
  • 3
    A practical introduction would assume you can research the tools you need, and int is a very basic tool (very unlikely it hasn't appeared yet if you're in chapter 6). Commented Nov 12, 2022 at 19:35
  • 1
    @juanpa, For example. No telling what topics the book has covered so far, and what type of contrived problems the author likes to set. (However it's not addition and subtraction, just the -1 function which is a lot simpler. Commented Nov 12, 2022 at 19:35
  • Give us a few minutes while we read the first 6 chapters of the book, to find out what has been mentioned :) Commented Nov 12, 2022 at 19:35

2 Answers 2

2

Ok i'll explain what I meant in the comments. If these are strings, not numbers, what is the pattern for subtraction by one? Replace the last digit with the digit just before it in the digit sequence, e.g. turn a final "5" into a "4". Unless it's a zero, in which case do that to the digit before and change the zero to a "9". Like this:

digits = "0123456789"
power = "25"     # from user input
last = power[-1]
if last != "0":
    # Subtract from the last digit, e.g. "2" + "4"
    newpower = power[:-1] + digits[digits.index(last)-1]
else:
    # Subtract one from the tens, e.g. 30 -> 29
    tens = power[-2]
    newpower = power[:-2] +  digits[digits.index(tens)-1] + "9"

It won't work for numbers ending in two zeros like 100, 200 etc., for that you'd need another level or you'd need to turn this into a tricky loop. Left as an exercise to the learner ;-)

Sign up to request clarification or add additional context in comments.

1 Comment

right, essentially, you are using a map to do your subtraction.
1

My The idea is to use the ASCII value of the digits from 0 to 9 start from 48 – 57.

original = input("Enter an x with a power: ")
part1 = original[2:]
part2 = original[0]
part3 = original[1]
c=0
part4 = original[2:]

for i in part4: 
 
    c = c * 10 + (ord(i) - 48) 
part4 = str(c-1)



derivative = part1 + part2 + part3 + part4
print(part4)
print("The derivative is", derivative)

output #

Enter an x with a power: x^25
The derivative is 25x^24

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.