Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

# Task: Given two real numbers a # find all the level-i (0 # import math #do not import any other packages def is_Palindrome(n): '''

# Task: Given two real numbers a

# find all the level-i (0

#

import math #do not import any other packages

def is_Palindrome(n):

''' decide if n is a level-0 Palindrome number or not, return True if yes, return False if no '''

def find_level(n, k):

''' decide the appropriate level of n, return i if n is a level-i Palindrome number (0

if n is not a Palindrome number for any level from 0 to k, return k+1

'''

#hint: the logic for the solution is as follows

# loop i from 0 to k,

# if n is of level-i, (do something); if not, (do something else)

# return k+1 if n is not Palindrome of any level from 0 to k

##

##if you prefer not to use the above functions and you have other ways to solve the following

##Palindrome(a, b, k=0) directly, you can code up what you prefer. (The instructor suggests

##using the above two functions is likely the most convenient way to solve this problem.)

##

def Palindrome(a, b, k=0):

''' Find all the level-i Palindrome numbers inside [a, b] for 0

floats and not necessarily integers.

Construct a dictionary, use integer i as its key (i=0, 1, ...k), the value associated with

the key i should be a list that contains all the level-i Palindrome numbers inside [a, b].

This dictinoary should also contain a key 'nop', its associated value should be a list that

contains all the remaining integers in [a, b] that are determined to be non-Palindrome number

for any level from 0 to k. (Make sure integers in each list are in increasing order.)

Return the constructed dictionary.

for example: Palindrome(50.1, 70.2, k=3) should return

{0: [55, 66], 1: [51, 52, 53, 54, 56, 60, 61, 62, 63, 65, 70], 2: [57, 58, 64, 67],

3: [59, 68], 'nop': [69]}.

'''

## do not modify the test code below

if __name__=='__main__':

dict1 = Palindrome(100.1, 150.2, k=5)

#print(dict1)

if dict1[0]!=[101, 111, 121, 131, 141] or \

dict1[1][:12]!=[102, 103, 104, 105, 106, 107, 108, 110, 112, 113, 114, 115] or \

dict1[1][-10:]!=[137, 138, 140, 142, 143, 144, 145, 146, 147, 148] or \

dict1[2]!=[109, 119, 129, 139, 149, 150] or any([dict1[i] != [] for i in [3,4,5,'nop']]):

print(' ***** Test1 failed for palindrome numbers ***** ')

else:

print('Great. Test1 passed for palindrome numbers ')

dict2 = Palindrome(500, 600, k=6)

#print(dict2)

if dict2[0]!= [505, 515, 525, 535, 545, 555, 565, 575, 585, 595] or \

dict2[1][:12]!=[500, 501, 502, 503, 504, 506, 510, 511, 512, 513, 514, 520] or \

dict2[1][-12:]!=[524, 530, 531, 532, 533, 534, 540, 541, 542, 543, 544, 600] or \

dict2[2][-12:]!=[556, 557, 558, 559, 566, 567, 568, 569, 576, 577, 578, 586] or \

dict2[3][-12:]!=[561, 564, 574, 580, 581, 582, 587, 588, 590, 596, 597, 598] or \

dict2[4]!=[539, 570, 571, 579, 591, 599] or dict2[5]!=[549, 562, 572, 594] or dict2[6]!=[] or \

dict2['nop']!=[563, 573, 583, 584, 589, 592, 593]:

print(' ***** Test2 failed for palindrome numbers ***** ')

else:

print('Great. Test2 passed for palindrome numbers ')

dict3 = Palindrome(1900, 2000.9, k=10)

#print(dict3)

if dict3[0]!=[1991] or dict3[1]!=[1900, 1901, 1902, 1903, 1904, 1905, 1906, 1907, 1908, 2000] or \

dict3[2][:12]!=[1909, 1910, 1911, 1912, 1913, 1919, 1920, 1921, 1922, 1923, 1929, 1930] or \

dict3[3][-12:]!=[1955, 1956, 1957, 1958, 1960, 1965, 1966, 1968, 1970, 1974, 1975, 1981] or \

dict3[4]!=[1935, 1944, 1948, 1959, 1961, 1962, 1963, 1982, 1983, 1985, 1989] or \

dict3[5]!=[1936, 1967, 1971, 1973, 1976, 1980, 1990, 1996] or \

dict3[6]!=[1969, 1979, 1986] or dict3[7]!=[1934, 1988, 1992, 1994, 1999] or \

dict3[8]!=[1993, 1995] or dict3[9]!=[1937] or dict3[10]!=[] or \

dict3['nop']!=[1927, 1945, 1946, 1947, 1972, 1977, 1978, 1984, 1987, 1997, 1998]:

print(' ***** Test3 failed for palindrome numbers ***** ')

else:

print('Great. Test3 passed for palindrome numbers ')

## choosing a relatively large k can tell what integers will 'never' be palindrome of any level

## (use even larger k should lead to same returned results)

a=1; b=1000;

dictn = Palindrome(a, b, k=1000)['nop']

#print('your non-palindrome in [{}, {}] of any level={}'.format(a, b, dictn['nop']))

if dictn[:4]!=[196, 295, 394, 493] or dictn[-9:-6]!=[592, 689, 691] or \

dictn[-6:]!=[788, 790, 879, 887, 978, 986]:

print(' ***** Test1 failed for NON-palindrome numbers ***** ')

else:

print('Great. Test1 passed for NON-palindrome numbers ')

a=2000; b=5000;

dictn = Palindrome(a, b, k=1000)['nop']

#print('your non-palindrome in [{}, {}] of any level={}'.format(a, b, dictn['nop']))

if dictn[:5]!=[2494, 2496, 2584, 2586, 2674] or \

dictn[-29:-20]!=[3765, 3853, 3855, 3943, 3945, 3995, 4079, 4169, 4259] or \

dictn[-10:]!=[4709, 4762, 4764, 4799, 4852, 4854, 4889, 4942, 4944, 4979]:

print(' ***** Test2 failed for NON-palindrome numbers ***** ')

else:

print('Great. Test2 passed for NON-palindrome numbers ')

image text in transcribed

b It vyp 0 e en i nb h hp gte neer nv 7 e] c d5nr n6oe 13 it 3 kt ean f3 nnt 1.1 Ah2 0 4 is u 2 T he b It vyp 0 e en i nb h hp gte neer nv 7 e] c d5nr n6oe 13 it 3 kt ean f3 nnt 1.1 Ah2 0 4 is u 2 T he

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Fundamentals Of Database Systems

Authors: Ramez Elmasri, Sham Navathe

4th Edition

0321122267, 978-0321122261

More Books

Students also viewed these Databases questions