Decimal to any other Number System converter.[Please check this code for required correction] | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Decimal to any other Number System converter.[Please check this code for required correction]

1)Please check this code for various set of inputs because I feel there are many loopholes in it and I am not able to figure it out. 2) Suggest any shorter and better method possible. 3) how to write code for base greater than 16 https://code.sololearn.com/cfo6jy6xnCb2/?ref=app

19th Mar 2022, 5:58 PM
Md Saif Ali
Md Saif Ali - avatar
6 Answers
+ 3
I'm adding answer for questions 1, 2 here as my code example above for answer 3 became too long 2. Shorter method: _________________________ x = input("your namber") b = input ("base system") letterList = [A, B, C, D, E, F] result = "" While (x > 0) { i = Math.mod(x, b) x = (x- t)/b If (i > 9) { i = letterList[i-10] } result = str(i) + result } ________________ 1. Avoid loopholes: To avoid loop hols make sure that: 0 < Base < 10+ letterList.length (see letterList in the code above)
20th Mar 2022, 5:51 PM
Lior Bakal
Lior Bakal - avatar
+ 2
20th Mar 2022, 5:57 PM
Md Saif Ali
Md Saif Ali - avatar
+ 2
Md Saif ali You asked: lior bakal 1. What if I run out of alphabets i.e after Z.. 3. I didn't get ur point 2 "for infinite base..." Answer: Infinite base is for when you run out of alphabet. You then need to present the numbers is your new (huge) base as list. For example in the base of 256 the number 300 would be 1, 44.
20th Mar 2022, 5:57 PM
Lior Bakal
Lior Bakal - avatar
+ 1
3. Use a list containing letters by order. Example code: _________________________ x = input("your namber") b = input ("base system") letterList = [A, B, C, D,...., Z] result = "" While (x > 0) { i = Math.mod(x, b) x = (x- t)/b If (i > 9) { i = letterList[i-10] } result = str(i) + result } ________________ Please note: 3.a.) Code limit base value This code is limited to some base, because at some point we run out of letters. Though you can always extend letterList to make it larger. maximum base = 10 + letterList.length 3.b.) For infinite base: To make it infinite size base would be to separate each value of the result by comma. For hex base result would be like: 43 = [2, 11] // on 16 base Then you can make even 256 base (such as used for RGB): 300 = [1, 44] // on 256 base Hope it was helpful Write me back if you have questions
20th Mar 2022, 5:38 PM
Lior Bakal
Lior Bakal - avatar
+ 1
lior bakalo 1. What if I run out of alphabets i.e after Z. 2. How to deal with negative as well as floating numbers conversion. 3. I didn't get ur point 2 "for infinite base..."
20th Mar 2022, 5:46 PM
Md Saif Ali
Md Saif Ali - avatar
+ 1
Md Saif ali You also asked: 2. How to deal with negative as well as floating numbers conversion Answer: This is indeed not addressed in my previous code. We would want to make this float an integer, then deal with it as before. Here's how: Assuming we have a number x 0 < x < 1 in the base system "b". you can use this code to extend my previous code: _________________________ x = input("your float, zero to one") b = input ("base system") letterList = [A, B, C, D,...., Z] result = "0." While (x > 0) { x = x×b i = x - Math.mod(x, 0) x =- i If (i > 9) { i = letterList[i-10] } result = result + str(I) } ____________________ Note on changes from previous integer code: A) x is multiplied by the base "b" each iteration to make is bigger then 1 but smaller then "b" B) we make sure to subtract mod (x, 0) from "i" so that i remains integer. C) we append str(i) from the right of "result" (either then left as done with integer input) For negative values add "-" to the left of "result".
20th Mar 2022, 6:26 PM
Lior Bakal
Lior Bakal - avatar