Python Basics簡単

Decimal to Hexadecimal Conversion

Detailed guide and Python implementation for the 'Decimal to Hexadecimal Conversion' problem.

問題提起

簡単

Write a function decimal_to_hex(n) that takes a non-negative integer n and returns a string representing its hexadecimal (base-16) equivalent using uppercase letters (A-F). Do not include leading zeros (except for input 0, which should return '0').

To convert, repeatedly divide by 16 and collect remainders (using A=10, B=11, ..., F=15) in reverse order.

制約
  • 0 <= n <= 10^6

Example 1
Input
decimal_to_hex(255)
Output
'FF'
Explanation

255 ÷ 16 = 15 remainder 15. 15 = F. So result is FF.

Example 2
Input
decimal_to_hex(26)
Output
'1A'
Explanation

26 ÷ 16 = 1 remainder 10. 10 = A. Result: 1A.

Example 3
Input
decimal_to_hex(100)
Output
'64'
Explanation

100 ÷ 16 = 6 remainder 4. Result: 64.

Need a Hint?
Consider using Numbers-specific data structures like sets or heaps.
Edge Cases to Watch
  • Empty input structures
  • Single element inputs
  • Large numerical bounds

解決する準備はできましたか?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

エディタで開く
Found this breakdown helpful?

PyRun is built and maintained by an independent solo developer. If this helped your interview prep, consider buying a coffee!

Buy me a coffee

推奨される Python リソース

関連するインタラクティブなチュートリアル、チートシート、コード比較で知識を深めてください。