Question

In: Computer Science

Python query for below: There are different ways of representing a number. Python’s function isdigit() checks...

Python query for below:

There are different ways of representing a number. Python’s function isdigit() checks whether the input string is a positive integer and returns True if it is, and False otherwise. You are to make a more generalised version of this function called isnumeric().

Just like isdigit(), this function will take input as a string and not only return a Boolean True or False depending on whether the string can be treated like a float or not, but will also return its float representation up to four decimal places if it is True.

Remember that numbers can be represented as fractions, negative integers, and float only. Check the sample test cases to understand this better.

Input: One line of string. The string will contain alphabets, numbers and symbols: '/' '.' '^'

Output: One line with True/False and then a space and the number with 4 decimal places if it is true and ‘nan’ if it is false


Sample Input 1: .2
Sample Output 1:
True 0.2000

Sample Input 2: 1 /. 2
Sample Output 2:
True 5.0000

Sample Input 3: 3^1.3
Sample Output 3:
True 4.1712

Sample Input 4: 3/2^3
Sample Output 4:
False NaN
Explanation:
if '^' and '/' are both present then give False. Since it is ambiguous, 2^5/5 can be seen as 2^(5/5) or (2^5)/5 likewise 3/2^3 can be seen as 3/(2^3) or (3/2)^3

Sample Input 5: 1.2.3
Sample Output 5:
False NaN

Sample Input 6: -2.
Sample Output 6:
True -2.0000

Solutions

Expert Solution

str="13"
digit1=""
digit2=""
operator=""
count=0
countForDotDigit2=0
countForDotDigit1=0

for element in str:
if element!='/' and element!='.' and element!='*' and element!='^' and element!='-' and element!='+' and element!='0' and element!='1' and element!='3' and element!='4' and element!='5' and element!='6' and element!='7' and element!='8' and element!='9' and element!='2':
print("False NaN")
break
elif element.isdigit() and count==0:
digit1+=element
elif element.isdigit() and count==1:
digit2+=element
elif element=='.' and count==1:
digit2+=element
countForDotDigit2=countForDotDigit2+1
if countForDotDigit2==2:
print("False NaN")
elif element=='.' and count==0:
digit1+=element
countForDotDigit1=countForDotDigit1+1
if countForDotDigit1==2:
print("False NaN")
elif element=='/' or element=='*' or element=='^' or element=='-' or element=='+':
count=count+1
operator=element

  
one=float(digit1)


if operator=='':
print("True ",'{:.4f}'.format(one))
elif operator=='+':
two=float(digit2)

result=one+two
print("True ", '{:.4f}'.format(result))
elif operator=='-':
two=float(digit2)

result=one-two
print("True ",'{:.4f}'.format(result))
elif operator=='*':
two=float(digit2)

result=one*two
print("True ", '{:.4f}'.format(result))
elif operator=='/':
two=float(digit2)

result=one/two
print("True ", '{:.4f}'.format(result))
elif operator=='^':
two=float(digit2)
result=one**two
print("True ", '{:.4f}'.format(result))

COMMENT DOWN FOR ANY QUERIES AND,

LEAVE A THUMBS UP IF THIS ANSWER HELPS YOU.


Related Solutions

Python please Write a function that takes a string as an argument checks whether it is...
Python please Write a function that takes a string as an argument checks whether it is a palindrome. A palindrome is a word that is the same spelt forwards or backwards. Use similar naming style e.g. name_pal. E.g. If we call the function as abc_pal(‘jason’) we should get FALSE and if we call it a abc_pal(‘pop’) we should get TRUE. Hint: define your function as abc_pal(str). This indicates that string will be passed. Next create two empty lists L1=[] and...
Python Question: Write a function that checks to see if an array of integers is sorted...
Python Question: Write a function that checks to see if an array of integers is sorted in an increasing fashion, returning true if it is, false otherwise. Test it with at least4 arrays - 2 sorted and 2 not sorted. Use a CSV formatted input file as described in the previous question to run your program through some tests, where again the filename is passed as an argument. Heres what I have so far: import sys # command line arguement...
What is the minimum number of bits for representing the opcode if there are 62 different...
What is the minimum number of bits for representing the opcode if there are 62 different instructions? Please explain it, thanks.
Use python write a function that checks the result of the blackjack game, given the sum...
Use python write a function that checks the result of the blackjack game, given the sum of all cards in player 1’s and player 2’s hand in each round. For the i-th round, the i-th index of player1 list represents the sum of all cards of player1, and the i-th index of player2 list represents player2's card sum. The i-th index of the returned list should be the winner's card sum. If both players' card sums go over 21 in...
6. Write a Python function that checks whether a passed string is palindrome or not. Note:-A...
6. Write a Python function that checks whether a passed string is palindrome or not. Note:-A palindrome is a word, phrase, or sequence that reads the same backward as forward . Some examples you may try: “madam” redder “race car” Eva, Can I Stab Bats In A Cave? If the argument passed is not a string, invoke an exception or an assertion and state in a comment which one you have chosen and why.
Write if logic in Python that checks if a variable named number is either greater than,...
Write if logic in Python that checks if a variable named number is either greater than, less than , or equal to 10 and prints out the number with the appropriate message, for example, if number is 6 you would output: number,"less than 10" or 6 less than 10
*Code in C* Write a function that checks if a number is a perfect cube. Write...
*Code in C* Write a function that checks if a number is a perfect cube. Write another function that calculates the integer cubic root. Under the main program: Prompt the user to input a number Tell the user if the number is a perfect cube or not Print the cubic root if the inputted number is a perfect cube.
Write a value returning function called isPrime. This function accepts integer number as parameter and checks...
Write a value returning function called isPrime. This function accepts integer number as parameter and checks whether it is prime or not. If the number is prime the function returns true. Otherwise, function returns false. A prime number is the number that can be divided by itself and 1 without any reminder, i.e. divisible by itself and 1 only. DO THIS USING C++ LANGUAGE .WITH UPTO CHAPTERS 5 (LOOP).
what are the different function features of python and javascript?
what are the different function features of python and javascript?
Python class DLLNode: """ Class representing a node in the doubly linked list implemented below. """...
Python class DLLNode: """ Class representing a node in the doubly linked list implemented below. """ def __init__(self, value, next=None, prev=None): """ Constructor @attribute value: the value to give this node @attribute next: the next node for this node @attribute prev: the previous node for this node """ self.__next = next self.__prev = prev self.__value = value def __repr__(self): return str(self.__value) def __str__(self): return str(self.__value) def get_value(self): """ Getter for value :return: the value of the node """ return self.__value...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT