Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Decimal to String Representation

EasyPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Decimal to String Representation. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

Max stores some content metadata identifiers as numeral strings in bases other than decimal. Given a string number and its base, return the integer value represented in decimal.

Formal Specification

Implement convert_to_decimal(number, base). number is a non-empty string containing digits 0-9 and uppercase letters A-Z, where A represents 10, B represents 11, and so on. The first character may be - for negative values. base is an integer from 2 through 36. Return the corresponding decimal value as a Python integer.

You may assume every digit is valid for the supplied base and that the result fits in Python's integer range. Do not use Python's built-in base-conversion functions such as int(number, base).

Constraints

  • 1 <= len(number) <= 10^5
  • 2 <= base <= 36
  • Digits are uppercase, except for an optional leading '-'
  • Every digit is valid in the supplied base
  • The represented value fits in Python's integer range

Function Signature

def convert_to_decimal(number, base):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output