FOR PYTHON 3


scrabble_number(num)


Description: Returns a scrabbled version of num by swapping the two digits of every consecutive pair of digits starting from the right.


Parameters: num (int)


Return value: int


Examples: scrabble_number(123456) → 214365


scrabble_number(4739917) → 4379971

Respuesta :

Answer:

Complete Python code with step by step comments for explanation are given below.

Python Code:

# creating a function named scrabble_number that will take num as input argument

def scrabble_number(num):

   # print the original number  

   print("The original number is: ",num)  

   # we can implement the required logic by using python built-in functions join() and zip()  

   scrambled = ''.join([char[1]+char[0] for char in zip(num[::2], num[1::2])])  

   # print the scrambled number

   print("The scrambled number is: " + str(scrambled))  

Driver code:

scrabble_number('123456')

Output:

The original number is: 123456

The scrambled number is: 214365

Ver imagen nafeesahmed