Question

8. Write a function is_nneg_float() that checks if string s denotes a non-negative floating point value...

8. Write a function is_nneg_float() that checks if string s denotes a non-negative floating point value (not in scientific notation). This function should return True if s contains (at most) one decimal point and one or more digits (and nothing else); and False otherwise. According to this definition an integer should return True. >>> is_nneg_float("2.15") True

>>> is_nneg_float("3.") True

>>> is_nneg_float(".5") True

>>> is_nneg_float("123") True >>> is_nneg_float("-12") False >>> is_nneg_float("1e10") False

0 0
Add a comment Improve this question Transcribed image text
Answer #1
def is_nneg_float(n):
    decimalPointCount = 0
    digitsCount = 0
    for x in n:
        if x=='.':
            decimalPointCount += 1
        elif x>='0' and x<='9':
            digitsCount += 1
        else:
            return False
    return decimalPointCount<=1 and digitsCount>=1

# Testing
print(is_nneg_float("2.15"))
print(is_nneg_float("3."))
print(is_nneg_float(".5"))
print(is_nneg_float("123"))
print(is_nneg_float("-12"))
print(is_nneg_float("1e10"))

Add a comment
Know the answer?
Add Answer to:
8. Write a function is_nneg_float() that checks if string s denotes a non-negative floating point value...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a function is_mirror(s) that takes as input a string s and returns True if s...

    Write a function is_mirror(s) that takes as input a string s and returns True if s is a mirrored string (i.e., a string that could have been produced by your mirror function) and False otherwise. Examples: >>> is_mirror('baconnocab') result: True >>> is_mirror('baconnoca') result: False Warning Your function should return a boolean value – either True or False, without any quotes around them. If you see quotes around the result when you make the calls above from the console, you must...

  • C++ Programming: Write a function that takes in a C++-style string s (from #include ), as...

    C++ Programming: Write a function that takes in a C++-style string s (from #include ), as well as an integer n, and then returns true if any substring of length n in s repeats itself, and false otherwise. For example, if s is "toe-to-toe" and n is 3, then the function would return true since "toe" occurs twice within s. However, if n were 4, the function would return false. As a second example, if s is "singing" and n...

  • ##8. A program contains the following function definition: ##def cube(num): ##return num * num * num...

    ##8. A program contains the following function definition: ##def cube(num): ##return num * num * num ##Write a statement that passes the value 4 to this function and assigns its return value ##to the variable result. ##9. Write a function named times_ten that accepts a number as an argument. When the ##function is called, it should return the value of its argument multiplied times 10. ##10. Write a function named is_valid_length that accepts a string and an integer as ##arguments....

  • c++ #include <iostream> #include <string> using namespace std; /* Write a function checkPrime such that input:...

    c++ #include <iostream> #include <string> using namespace std; /* Write a function checkPrime such that input: an int output: boolean function: Return true if the number is a prime number and return false otherwise */ //TO DO ************************************** /* Write a function checkPrime such that input: an array of int and size of array output: nothing function: Display the prime numbers of the array */ //TO DO ************************************** /* Write a function SumofPrimes such that input: an int output: nothing...

  • #Write a function called check_formula. The check_formula #function should take as input one parameter, a string....

    #Write a function called check_formula. The check_formula #function should take as input one parameter, a string. It #should return True if the string holds a correctly #formatted arithmetic integer formula according to the rules #below, or False if it does not. # #For this problem, here are the rules that define a #correctly-formatted arithmetic string: # # - The only characters in the string should be digits or the five arithmetic operators: +, -, *, /, and =. Any other...

  • Write a recursive version of the palindrome function. It should ignore non-letters and case. (Hint: use...

    Write a recursive version of the palindrome function. It should ignore non-letters and case. (Hint: use isalpha() .) The function signature is: def is_palindrome(s): """ ------------------------------------------------------- Recursively determines if s is a palindrome. Ignores non-letters and case. Use: palindrome = is_palindrome(s) ------------------------------------------------------- Parameters: s - a string (str) Returns: palindrome - True if s is a palindrome, False otherwise (boolean) ------------------------------------------------------- """

  • Write a C/C++ program that simulate a menu based binary number calculator. This calculate shall have the following three...

    Write a C/C++ program that simulate a menu based binary number calculator. This calculate shall have the following three functionalities: Covert a binary string to corresponding positive integers Convert a positive integer to its binary representation Add two binary numbers, both numbers are represented as a string of 0s and 1s To reduce student work load, a start file CSCIProjOneHandout.cpp is given. In this file, the structure of the program has been established. The students only need to implement the...

  • Q1)(20p)Write a static member function called removeLongestRepeatingSegment that takes a String a...

    Q1)(20p)Write a static member function called removeLongestRepeatingSegment that takes a String argument. The method will return a new String by removing the logest segment that contains consequtively repeating characters. public static void main(String []args){ String s=”1111222223333311111111444455552222”; String res= removeLongestRepeatingSegment(s); will return “11112222233333444455552222” } public static String removeLongestRepeatingSegment(String s){ --------------- Q2)15p)Given a list of objects stored (in ascending order) in a sorted linked list, implement member method which performs search as efficiently as possible for an object based on a key....

  • 1.) Write a recursive function named isPalindrome that will take a single string as an argument...

    1.) Write a recursive function named isPalindrome that will take a single string as an argument and determine if the argument is a palindrome. The function must return True if the argument is a palindrome, False otherwise. Recall that any string is a palindrome if its first and last characters are the same and the rest of it is also a palindrome (therefore, a single character is a palindrome): 2.) Consider a text file named samplestrings.txt that contains a collection...

  • 1.1. Write a function named "areFirstTwoTheSame AsLast TwoChars" that accepts a string. It returns true if...

    1.1. Write a function named "areFirstTwoTheSame AsLast TwoChars" that accepts a string. It returns true if the first two characters and the last two characters of the string are the same. It returns false otherwise. In addition, if the string is empty or has only one character, it also returns false. For example, these are the strings with their expected return values false falsc "AB" true "ABA" false "ABAB" trus "ABBA" false "ABCABC false "ABCCAB" true 1.2 Write a function...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT