Question

In: Computer Science

Is there anyway I could get an example to set this python function set up? any...

Is there anyway I could get an example to set this python function set up? any help would be amazing.

basetonum (S, B) --- int:

  1. This function accepts as input a string S and a base B (int) where S represents a number in base B where B is between 2 and 10 inclusive. It should then return an integer in base 10 representing the same number as S. It should output 0 when S is the empty string. This function is the inverse of the previous function numtobase().

  2. Parameters: S (string), B (int)

  3. Returns : integer

  4. The function displays nothing.

  5. Steps to convert any base system to decimal:

    Step 1 − Determine the column (positional) value of each digit (this depends on the position of the digit and the base of the number system).
    Step 2 − Multiply the obtained column values (in Step 1) by the digits in the corresponding columns.

    Step 3 − Sum the products calculated in Step 2. The total is the equivalent value in decimal.

    Example with S = '11101' and B = 2 Binary Number − 11101 Calculating Decimal Equivalent −

    Nb43210 Step 1 11101 (1×2) + (1×2) + (1×2) + (0×2) + (1×2)

    Step2 11101 16+8+4+0+1 Step 3 11101 29

       

Binary Number − 11101 = Decimal Number – 29

Again, the key is to ask yourself... what has to change in order to output base B instead of base 2?

In [1]: basetonum('11101', 2) Out[1]: 29

In [2]: basetonum('', 4) Out[2]: 0

Solutions

Expert Solution

Below given the python solution and the sample output to convert any base system to decimal.

val(c) and basetonum(S,B) are the two functions used in the program to convert any base system to decimal.


Related Solutions

I need the code in python where I can encrypt and decrypt any plaintext. For example,...
I need the code in python where I can encrypt and decrypt any plaintext. For example, the plaintext "hello" from each of these Block Cipher modes of Operation. Electronic Code Block Mode (ECB) Cipher block Mode (CBC) Cipher Feedback Mode (CFB) Output feedback Mode (OFB) Counter Mode (CTR) Here is an example, Affine cipher expressed in C. Encryption: char cipher(unsigned char block, char key) { return (key+11*block) } Decryption: char invcipher(unsigned char block, char key) { return (163*(block-key+256)) }
How to set up experimental design for 3 factors at 4 different levels. I get in...
How to set up experimental design for 3 factors at 4 different levels. I get in total I should end up with 81 experimental runs. Can you help me to determine how to distribute these 4 levels across 3 factors?
Could I please get an explicit example of a conformal transformation represented through only rotations on...
Could I please get an explicit example of a conformal transformation represented through only rotations on the riemann sphere to help show mobius transformations?
Please fix this python script, I keep getting a return outside function error and cannot get...
Please fix this python script, I keep getting a return outside function error and cannot get it to run: def caesar(plainText, shift):     cipherText = "" for char in plainText:     newChar = ord(char) + shift     if newChar > 128:       newChar = newChar % 128     finalChar = chr(newChar)     cipherText += finalChar return cipherText text = input("Enter a message: "); s = input("Enter the distance value: "); print (caesar(text, int(s)));
Write below in Python Get user name from keyboard using input() function (Example username = input("Enter...
Write below in Python Get user name from keyboard using input() function (Example username = input("Enter username:") A: What is your name? B: My name is XXXX. B: What is yours? A: My name is XXXX. A: Nice to meet you. B: Nice to meet you, too. Use print statement and use input() function
Could I please get explainations and working out as to how to get to the answers....
Could I please get explainations and working out as to how to get to the answers. Note that  is where part of the answer goes. 1. Consider the following ArrayList list: ArrayList list = new ArrayList(Arrays.asList(10,70,20,90)); //list = [10, 70, 20, 90] Complete the following statements so that list contains the items [50, 70, 20] Do NOT include spaces in your answers. list.remove(  ); list.  (  , 50); 2. Assume there exists an ArrayList list that...
Could I please get explainations and working out as to how to get to the answers....
Could I please get explainations and working out as to how to get to the answers. Note that  is where part of the answer goes. 1. Write a statement that removes the first occurrence of item 10 (if any) from an ArrayList of Integer objects, data. data.remove(  ); 2. Complete the following function that returns true if ArrayList list contains any positive (more than 0) item, false otherwise. public static  containsPositive(ArrayList list) { for(int i=0; i <...
Could I please get explainations and working out as to how to get to the answers....
Could I please get explainations and working out as to how to get to the answers. Note that  is where part of the answer goes. 1. Complete the following function that returns true if ArrayLists list1 and list2 are exactly the same in terms of contents (same items in same order), false otherwise. public static  sameSame(ArrayList<Integer> list1, ArrayList<Integer> list2) { if(list1 == null || list2 == null) return false; if(  !=  ) return false; for(int i=0;...
I was wondering if I could get a step by step process to answer this question?
I was wondering if I could get a step by step process to answer this question?
Python pls Create a function dict_sum. This function takes a dictionary and sums up the values...
Python pls Create a function dict_sum. This function takes a dictionary and sums up the values in the dictionary. For example: dict1 = {1: {'una': 5, 'dos': 7, 'tres': 9, 'quar' : 11}, 2: {'dos':2, 'quar':4}, 3:{'una': 3, 'tres': 5}, 4:{'cin': 6}, 5:{'tres': 7 , 'cin': 8}} dict2 = {300:{'s': 300}, 400:{'s': 100, 'g': 100, 'p': 100}, 500: {'s': 50 ,'m': 400, 'p':30, 'i': 50}, 600: {'s': 40, 'i': 400}, 700: {'m': 100, 'p': 50}} def dict_sum(db): should give output...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT