Question

In: Computer Science

def change_type(info: List[list]) -> None: """ Modify info to a float if and only if it...

def change_type(info: List[list]) -> None:
""" Modify info to a float if and only if it represents a number that is not
a whole number(a float), and convert info to an int if and only if it represents a
whole number, keep everythingelse as a string.

>>> i = [['apple', '888', 'School', '111.1']]
>>> change_type(i)
>>> i

  [['apple', 888, 'School', '111.1']]

Please help! Write as little code as possible!

Please only use if statements, nested loop and lists to solve this question.

Solutions

Expert Solution

def change_type(info):
    newList = []

    # iterating list through sub lists
    for subList in info:

        # initializing empty list
        temp = []
        # iterating though every element in sub list
        for element in subList:

            # checking if number is numeric or not
            # if numeric converted into integer and appended
            # else numeric is converted to float and appended
            if element.isnumeric():
                if int(element) >= 0:
                    temp.append(int(element))
                else:
                    temp.append(float(element))
            else:
                temp.append(element)

        # appending sublist to newList
        newList.append(temp)
        # returning newlist

    return newList

Code

Output


Related Solutions

from partition import partition def quicksort(a: list, l: int, u: int) -> None: '''Sort the given...
from partition import partition def quicksort(a: list, l: int, u: int) -> None: '''Sort the given list a in non-descending order. Precondition: 0 <= l and u < len(a)''' if l < u: mid = (l + u) // 2 three = [a[l], a[mid], a[u]] three.sort() if three[1] == a[l]: pivot_loc = l elif three[1] == a[u]: pivot_loc = u else: pivot_loc = mid a[u], a[pivot_loc] = a[pivot_loc], a[u] pivot = a[u] i = partition(a, l, u - 1, pivot)...
modify the program to cast vptr as a float and as a double build and run...
modify the program to cast vptr as a float and as a double build and run your program THIS IS THE PROGRAM CODE: #include <stdio.h> void main (void) { int intval = 255958283; void *vptr = &intval; printf ("The value at vptr as an int is %d\n", *((int *) vptr)); printf ("The value at vptr as a char is %d\n", *((char *) vptr)); }
class Board: def __init__(self): self.pieces = [[None for i in range(8)] for j in range(8)] def...
class Board: def __init__(self): self.pieces = [[None for i in range(8)] for j in range(8)] def __str__(self): s = "" for j in range(7, -1, -1): #for each row for i in range(8): # for each column if self.pieces[j][i] is None: # if self.pieces[j][i] is None s += "." # populate the row with '.' val for each column else: s += self.pieces [j][i].get_symbol() s += "\n" #after each row add a new line return s # return after iterating...
''' File: pyPatientLL.py Author: JD ''' class Node: #ADT        def __init__(self, p = None):             ...
''' File: pyPatientLL.py Author: JD ''' class Node: #ADT        def __init__(self, p = None):              self.name = ""              self.ss = self.age = int(0)              self.smoker = self.HBP = self.HFD = self.points = int(0)              self.link = None              #if list not empty              if p != None:                     p.link = self        ptrFront = ptrEnd = None choice = int(0) def menu():        print( "\n\tLL Health Clinic\n\n")        print( "1. New patient\n")        print( "2. View patient by...
Python Q1. def silly(stuff, more_stuff=None): data = [] for i, thing in enumerate(stuff): if thing >=...
Python Q1. def silly(stuff, more_stuff=None): data = [] for i, thing in enumerate(stuff): if thing >= 0 or i < 2: data.append(thing + i) print(len(stuff)) if more_stuff is not None: data += more_stuff print(data) Write a Python statement that calls silly and results in the output (from within silly): 4 [5, 3, 1, 5, -3] stuff = [5, 2, 0, 2] more_stuff = [-3] silly(stuff, more_stuff) Q2. Function silly2 is defined as: def silly2(nums, indices, extra=None): print(len(nums), len(indices)) new_indices =...
Python file def calci(): grade = float(input("Enter your grade:")) while (grade <= 4): if (grade >=...
Python file def calci(): grade = float(input("Enter your grade:")) while (grade <= 4): if (grade >= 0.00 and grade <= 0.67): print("F") break elif (grade >= 0.67 and grade <= 0.99): print("D-") break elif (grade >= 1.00 and grade <= 1.32): print("D") break elif (grade >= 1.33 and grade <= 1.66): print("D+") break elif (grade >= 1.67 and grade <= 1.99): print("C-") break elif (grade >= 2.00 and grade <= 2.32): print("C") break elif (grade >= 2.33 and grade <=...
C++ Given vector<float> vec; Using a ranged for loop, modify each value in vector to cube...
C++ Given vector<float> vec; Using a ranged for loop, modify each value in vector to cube and subtract 12.5
Python class LinkedNode: # DO NOT MODIFY THIS CLASS # __slots__ = 'value', 'next' def __init__(self,...
Python class LinkedNode: # DO NOT MODIFY THIS CLASS # __slots__ = 'value', 'next' def __init__(self, value, next=None): """ DO NOT EDIT Initialize a node :param value: value of the node :param next: pointer to the next node in the LinkedList, default is None """ self.value = value # element at the node self.next = next # reference to next node in the LinkedList def __repr__(self): """ DO NOT EDIT String representation of a node :return: string of value """...
In phyton, lab 5 info. def is_dna(one): sequence = input().upper() valid_dna = "ACGT" sequence = sequence.replace("...
In phyton, lab 5 info. def is_dna(one): sequence = input().upper() valid_dna = "ACGT" sequence = sequence.replace(" ", "") for i in sequence: if i in valid_dna: count = 1 else: count=0 if count==1: print("This is a valid DNA sequence.") else: print("This is an invalid DNA sequence") 5 def is_valid(dna): for one in dna: if not is_dna(one): return False return true print(is_valid("AGCTAGCAAGTCTT")) #prints true print(is_dna("ACGTTGGC")) #prints false 1. Write a function called 'transcribe' that takes a DNA sequence as an argument....
def main():     print('This program determines whether you have hypertension.')     systolic_pressure = float(input('Enter your systolic...
def main():     print('This program determines whether you have hypertension.')     systolic_pressure = float(input('Enter your systolic pressure: '))     diastolic_pressure = float(input('Enter your diastolic pressure: '))     # insert statement here to call the hypertension_tester function def hypertension_tester(dp, sp):     if sp >= 140 or dp >= 90:         print('You have hypertension')     else:         print('You do not have hypertension') main() A person has hypertension if systolic pressure is 140 or above or diastolic pressure is 90 or above. Which of the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT