예제 #1
0
def main():
    """
    test function
    :return: none
    """
    thousand_primes = islice((x for x in count() if is_prime(x)), 1000) # Returns a list of first 1000 primes
    print(thousand_primes, type(thousand_primes))
    print("list of first thousand prime numbers", list(thousand_primes))
    # Note: if you need to use the object again, you need to regenerate it, or you get a 0 value.
    thousand_primes = islice((x for x in count() if is_prime(x)), 1000)
    print("Sum of first thousand prime numbers", sum(thousand_primes))
# other built-ins used with itertools: any ("or"), all ("and")
    print(any([False, False, True]))
    print(all([False, False, True]))
    print("Are there any primes between 1328-1361?", any(is_prime(x) for x in (range(1328, 1362))),
          list(x for x in range(1328, 1362) if is_prime(x)))
    # Check if all names in an iterable are in the title form: first letter capitalized
    names = ["London", "New York", "ogden"]
    print(all(name == name.title() for name in names))
    # Another built-in: zip
    sunday = [2, 2, 5, 7, 9, 10, 9, 6, 4, 4]
    monday = [12, 14, 14, 15, 15, 16, 13, 10, 9]
    tuesday = [13, 14, 15, 15, 16, 17, 16, 16, 12, 12]
#    wednesday = [15, 15, 16]
    # monday = [12, 14, 14, 15, 15, 16, 13, 10, 9]
    # monday = [12, 14, 14, 15, 15, 16, 13, 10, 9]
    # Calculate min, max, ave for 3 days
    # {:4.1f} => 4 chars width, 1 decimal precision floating point
    for temps in zip(sunday, monday, tuesday):
        print("min={:4.1f}, max={:4.1f}, avg={:4.1f}".format(
            min(temps), max(temps), sum(temps)/len(temps) ))

    # Chain
    all_temps = chain(sunday, monday, tuesday)
    print("All temps > 0? ", all(t>0 for t in all_temps))
예제 #2
0
def main():
    """

    :return:
    """
    thousand_primes = islice((x for x in count() if is_prime(x)), 1000)
    print(thousand_primes, type(thousand_primes))
    print("List og first 1k prime numbers:", list(thousand_primes))
    thousand_primes = islice((x for x in count() if is_prime(x)), 1000)
    print("List og first 1k prime numbers:", sum(thousand_primes))
    #other built_ins use with itertools: any, all
    print(any([False, False, False, True]))
    print(all([False, False, True]))
    print(any([is_prime(x) for x in range(1328, 1362)]))
    names = ["London", "New York", "ogden"]
    print(all([s.istitle() for s in names]))
    #another built-in: zip()
    monday = [12, 14, 14, 15, 15, 16, 15, 13, 10, 9]
    tuesday = [13, 14, 15, 15, 16, 17, 16, 16, 12, 12]
    sunday = [2, 2, 5, 7, 9, 10, 9, 6, 4, 4]

    # for temps in zip(monday, tuesday, sunday):
    #     print("min={:4.1f}, max={4.1f}, avg={4.1f}".format(
    #         min(temps), max(temps), sum(temps)/len(temps)
    #     ))
    all_temps = chain(sunday, monday, tuesday)
    print("All temperatures > 0", all(t > 0 for t in all_temps))
예제 #3
0
def main():
    """
    Test function
    :return: 
    """
    thousand_primes = islice((x for x in count() if is_prime(x)), 1000)
    print(thousand_primes, type(thousand_primes))
    print("list of fist 1k prime numbers", list(thousand_primes))
    # in order to use thousand primes you have to regenerate it
    thousand_primes = islice((x for x in count() if is_prime(x)), 1000)
    print("list of fist 1k prime numbers", sum(thousand_primes))
    # other built-ins use with itertools: any, all
    print(any([False, False, True]))
    print(all([False, False, True]))
    print(any( is_prime(x) for x in range(1328,1362)))

    names = ['London', 'New York', 'Ogden']
    print(all(name == name.title() for name in names))
    # another built in: zip
    sunday = [2,2,5,7,9,10,9,6,4,4]
    monday = [12,14,14,15,15,16,15,13,10,9]
    tuesday = [13,14,15,15,16,17,16,16,12,12]
    for temps in zip(sunday,monday,tuesday):
        print(min(temps),max(temps),sum(temps)/len(temps))

    #chaining
    all_temps = chain(sunday,monday,tuesday)
    print("all temps > 0", all(t>0 for t in all_temps))
예제 #4
0
def main():
    """
    Test function
    :return:
    """
    thousand_primes = islice((x for x in count() if is_prime(x)), 1000)
    print(thousand_primes, type(thousand_primes))
    print("List of first 1K prime numbers:", list(thousand_primes))
    # Note: If you need to use the object again, you need to re-generate it
    thousand_primes = islice((x for x in count() if is_prime(x)), 1000)
    print("Sum of first 1K prime numbers:", sum(thousand_primes))
    # Other built-ins use with itertools: any ("or"), all ("and")
    print(any([False, False, True]))
    print(all([False, False, True]))
    print("Are there prime numbers between 1328 and 1361?",
          any(is_prime(x) for x in range(1328, 1362)),
          list(x for x in range(1328, 1362) if is_prime(x)))
    # Check if all names in an iterable are in title form: First letter capitalize
    names = ["London", "New York", "ogden"]
    print(all(name == name.title() for name in names))
    # Another built-in: zip()
    sunday = [2, 2, 5, 7, 9, 10, 9, 6, 4, 4]
    monday = [12, 14, 14, 15, 15, 16, 15, 13, 10, 9]
    tuesday = [13, 14, 15, 15, 16, 17, 16, 16, 12, 12]
    # wednesday = [12, 12]
    # Calculate the minimum, maximum, and average of all points
    # {:6.1f} => 6 chars width, 1 decimal precision floating point
    for temps in zip(sunday, monday, tuesday):
        print("min={:6.1f}, max={:6.1f}, avg={:6.1f}".format(
            min(temps), max(temps),
            sum(temps) / len(temps)))
    # chain from itertools
    all_temps = chain(sunday, monday, tuesday)
    print("All temperatures > 0", all(t > 0 for t in all_temps))
def main():
    """
    Test function
    :return: 
    """
    # generate the first 1000 primes
    thousand_primes = islice((x for x in count() if  is_prime(x)), 1000)
    print(thousand_primes, type(thousand_primes))
    print("List of first 1K prime numbers:", list(thousand_primes))
    # what if we want to add them? wrap it all up with a sum, need to regenerate you data set
    thousand_primes = islice((x for x in count() if is_prime(x)), 1000)
    print("Sum of first 1K prime numbers:", sum(thousand_primes))
    # other built-ine use with itertools: like any ("or"), or all ("and") : boolean statements T of F
    print(any([False, False, True])) # should get true back -> True: : can use to test for empty strings
    print(all([False, False, True])) # -> False

    # Lets say we have the range 1328, 1361 are there any prime numbers in this range?
    print("Are there prime numbers between 1328 and 1361?",
          any(is_prime(x) for x in range(1328, 1362))) # -> True : the is_prime is producing a list of booleans
    # if we used ( x for x in... it would produce a list of integers (warning: false positive any nonzero int is true
    # Print the prime numbers within that range
    print("Are there prime numbers between 1328 and 1361?",
          any(is_prime(x) for x in range(1328, 1362)), list(x for x in range(1328, 1362) if is_prime(x)))
    # -> Are there prime numbers between 1328 and 1361? True [1361]

    # Check if all names in the iterable are in title form: First letter capitalized
    names = ["London", "New York", "Ogden"] # all must be first letter CAPS
    print(all(name == name.title() for name in names)) # -> True
    names = ["London", "New York", "ogden"] # all must be first letter CAPS
    print(all(name == name.title() for name in names))  # -> False

    # Another built-in: zip():
    sunday  = [2, 2, 5, 7, 9, 10, 9, 6, 4, 4]
    monday  = [12, 14, 14, 15, 15, 16, 15, 13, 10, 9] # degrees in celsius
    tuesday = [13, 14, 15, 15, 16, 17, 16, 16, 12, 12]
    # lets iterate over data set: compare first M to first T without keeping track of the index
    for item in zip(monday, tuesday): # -> Prints as tuples
        print(item)
    # what was the average temp for each reading? Unpack the tuples
    for mon, tues in zip(monday, tuesday):
        print("average = ", (mon + tues/2))
        """ ->
        average =  18.5
        average =  21.0
        average =  21.5 ...
        """
        # calculate the min, max and avg of all points.
    for temps in zip(sunday, monday, tuesday):
               print("min = {:4.1f}", "max = {:4.1f}", "avg = {:4.1f}".format(
            min(temps), max(temps), sum(temps)/len(temps)))
        # {:indent 4(WIDTH OF FIELD)    . one floating point
        # what happens when your data set is not even. it stops when it does not have an even set to continue with
        # example: if we added wednesday = [2, 2] all of our sets would only report 2 values

    # Chain from itertools: combine all data sets
    all_temps = chain(sunday, monday, tuesday)
    print("All temperatures > 0", all(t > 0 for t in all_temps))
예제 #6
0
def main():
    """
    test function for words library
    :return: nohing
    """
    thousand_primes = islice((x for x in count() if is_prime(x)), 1000)
    print(thousand_primes, type(thousand_primes))
    print ("List of first 1K fprime numbers:", list(thousand_primes))
    # Note: if you need to use the object again, you need to re-generate it
    thousand_primes = islice((x for x in count() if is_prime(x)), 1000)
    print("Sum of first 1K fprime numbers:", sum(thousand_primes))

    # Other build-ins use with itertools: any (OR), all (AND)
    print(any([False, False, True]))
    print(all([False, False, True]))

    print(any(is_prime(x) for x in range(1352, 1360)))
def main():
    """

    :return:
    """
    #list with one million square numbers
    m_sq = (x for x in range(1, 10001) if (is_prime(x)))
    print(m_sq, type(m_sq))
    print(sum(m_sq))
예제 #8
0
def main():
    """
    Test function
    :return:
    """
    thousand_primes = islice((x for x in count() if is_prime(x)), 1000)
    print(thousand_primes, type(thousand_primes))
    print("List of first 1K prime numbers:", list(thousand_primes))
    # Note: If you need to use the object again, you need to re-generate it
    thousand_primes = islice((x for x in count() if is_prime(x)), 1000)
    print("Sum of first 1K prime numbers:", sum(thousand_primes))

    # Other built-ins to use with itertools: any (think "or"), all (think "and")
    print(any([False, False, True]))
    print(all([False, False, True]))
    print()

# Assignment: Do I have any prime numbers in the range of 1328, 1361?
# my guess: use the range command
# this should return a TRUE
    print("Are there prime numbers between 1328 and 1361?",
          any(is_prime(x) for x in range(1328, 1362)),
          list(x for x in range(1328, 1362) if is_prime(x)))


    # Check if all names in an iterable are in title form: First letter capitalized
    names = ["London", "New York", "Ogden"]
    print(all(name == name.title() for name in names)) # name == name.title will yield true or false
    print()

    # Another built-in: zip
    sunday = [2, 2, 5, 7, 9, 10, 9, 6, 4, 4]
    monday = [12, 14, 14, 15, 15, 16, 15, 13, 10, 9]
    tuesday = [13, 14, 15, 15, 16, 17, 16, 16, 12, 12]
    # Calculate the min, max, and average of all points
    # (:6.1f) => 6 chars width, 1 decimal precision floating point
    for temps in zip(sunday, monday, tuesday):
        # print("Average =", (mon + tue/2)   We moditied this from what we did earlier
        print("min={:4.1f}, max={:4.1f}, ave={:6.1f}".format(
            min(temps), max(temps), sum(temps)/len(temps)))

    # chain from intertools
    all_temps = chain(sunday, monday, tuesday)
    print("All temperatures > 0", all(t > 0 for t in all_temps))
예제 #9
0
def my_code():
    """
    Test function
    :return: 
    """
    thousand_primes = islice(
        (x for x in count() if is_prime(x)),
        1000)  # this is not computationally performed here
    print(thousand_primes, type(thousand_primes))  # doesn't execute

    print("List of first 1K prime numbers:", list(thousand_primes))

    # Need to re-generate object
    thousand_primes = islice((x for x in count() if is_prime(x)), 1000)
    print("Sum of first 1K prime numbers:", sum(thousand_primes))

    # other built-in for use with itertools: any, all
    print(any([False, False, True]))  # OR gate
    print(all([False, False, True]))  # AND gate

    # print(list(islice(x for x in range(1328, 1362) if is_prime(x))), )
    print("List of the prime numbers between 1328 and 1400:",
          list(x for x in range(1328, 1400) if is_prime(x)))
    print("Are there prime numbers between 1328 and 1362?",
          any(is_prime(x) for x in range(1328, 1362)))

    names = ["London", "New york", "Ogden"]
    print(all(name == name.title() for name in names))

    # Another built-in: zip()
    Monday = [12, 14, 14, 15, 15, 16, 15, 13, 10, 9]  # Temp in Celsius
    Tuesday = [13, 14, 15, 15, 16, 17, 16, 16, 12, 12]
    Wednesday = [2, 2, 5, 5, 7, 5, 5, 4, 4, 3]

    format_ = ''
    for temps in zip(Monday, Tuesday, Wednesday):
        print(
            "Temperatures in Celsius: max={:6.1f}, min={:6.1f} and  average={:6.1f} "
            .format(max(temps), min(temps),
                    statistics.mean(temps)))  #statistics.mean(temps)

    all_temps = chain(Monday, Tuesday, Wednesday)
    print("All temperature > 0? ", all(t > 0 for t in all_temps))
    print("All temperature > 0? ", all(t > 0 for t in temps))
예제 #10
0
def main():
    """
    Test function
    :return: 
    """
    thousand_primes = islice((x for x in count() if is_prime(x)), 1000)
    print(thousand_primes, type(thousand_primes))
    print("List of the first 1K prime numbers:", list(thousand_primes))
    # Note: If you need to use the object again you need to re-generate it
    thousand_primes = islice((x for x in count() if is_prime(x)), 1000)
    print("Sum of the first 1K prime numbers:", sum(thousand_primes))

    # Other built-ins use with itertools: any, all
    print(any([False, False, True]))    # like an OR
    print(all([False, False, True]))    # like an AND

    # Want to find if there are any prime numbers in the range: 1328-1361
    # sub_prime = islice(x for x in range(1328,1363)) <-- this way my attempt
    print("Are there prime numbers between 1328 and 1361", any(is_prime(x) for x in range(1328, 1362)), list(x for x in range(1328, 1362) if is_prime(x)))

    # Check if all names in an iterable are in title form: i.e. First letter capitalized
    names = ["London", "New York", "Ogden"]
    print(all(name == name.title() for name in names))

    # Another built-in: zip()
    sunday  = [2, 2, 5, 7, 9, 10, 9, 6, 4, 4]
    monday  = [12, 14, 14, 15, 15, 16, 15, 13, 10, 9]
    tuesday = [13, 14, 15, 15, 16, 17, 16, 16, 12, 12]

    for item in zip(monday, tuesday):
        print(item)
    # Want to know the average temp for each set of readings, i.e. the average of the first monday temp and first tuesday temp, etc.
    for mon, tue in zip(monday, tuesday):
        print("average =", (mon + tue)/2)

    # Calculate the minimum, maximum, and average of all points
    # 4 = total width of the field (including decimal point), 1 = precision, f = floating point
    for temps in zip(sunday, monday, tuesday):
        print("min={:4.1f}, max={:4.1f}, avg={:4.1f}".format(min(temps), max(temps), sum(temps)/len(temps)))

    # chain
    all_temps = chain(sunday, monday, tuesday)
    print("All temperatures > 0:", all(t > 0 for t in all_temps))
def my_code():
    """
    Test function
    :return: 
    """
    # list with first 1 million square numbers
    m_sq = (x*x for x in range(1, 1000))
    print(m_sq, type(m_sq))
    print("The sum of the first 1M squares is:",sum(m_sq))
    print("The sum of the first 1M squares is:",sum(x*x for x in range(1, 1000001)))

    print("The sum of the prime numbers below 1M is: ",
          sum(x for x in range(1, 10001) if is_prime(x)))
예제 #12
0
def main():
    """
    Test function
    :return: 
    """
    # list with first 1 million square numbers
    m_sq = (x * x for x in range(1, 1000001))
    print(m_sq, type(m_sq))
    print("sum of the 1st 1 mill squares is ", sum(m_sq))
    print("sum of the 1st 1 mill squares is ",
          sum((x * x for x in range(1, 1000001))))
    print("sum of the prime numbers between 1 and 1 mil",
          sum(x for x in range(10001) if is_prime(x)))
예제 #13
0
def main():
    """
    test function
    :return: none
    """
    # list with first 1 million sq numbers
    m_sq = (x * x for x in range(1, 1000001))
    print(m_sq, type(m_sq))
    print('sum of first 1M squares is:', sum(m_sq))
    print('sum of first 1M squares is:', sum(x * x for x in range(1, 1000001)))
    # sum of primes from 1 - 10K
    print('sum of prime numbers from 1 to 10K:',
          sum(x for x in range(1, 10001) if is_prime(x)))
예제 #14
0
def main():
    """
    Test function
    :return: Nothing
    """
    # generate the first 1000 primes. List is one time use, must be regenerated to do multiple things
    thousand_primes = islice((x for x in count() if is_prime(x)), 1000)
    print(thousand_primes, type(thousand_primes))
    print("List of first 1K prime numbers: ", list(thousand_primes))
    # Note: If you need to use the object again, you need to re-generate it
    thousand_primes = islice((x for x in count() if is_prime(x)), 1000)
    print("Sum of first 1K prime numbers: ", sum(thousand_primes))
    # Other built-ins use with itertools: any ("or"), all ("and")
    print(any([False, False, True]))  # Like an or
    print(all([False, False, True]))  # Like an and
    # 1361 is the prime number
    print("Are there prime numbers between 1328 and 1361?: ",
          any(is_prime(x) for x in range(1328, 1362) if is_prime(x)))
    # check if all names in an iterable are in title form: first letter capitalize
    names = ['London', "New York", 'Ogden']
    print(all(name == name.title() for name in names))
    # Another built-in: zip()
    sunday = [2, 2, 5, 7, 9, 10, 9, 6, 4, 4]
    monday = [12, 14, 14, 15, 15, 16, 15, 13, 10, 9]
    tuesday = [13, 14, 15, 15, 16, 17, 16, 16, 12, 12]
    # wednesday = [12, 12]  # if the data isn't the same length it will only evaluate these two data points
    # Calculate the minimum, maximum, and average of all points
    # {:6.1f} => 6 chars width, 1 decimal precision floating point
    for temps in zip(
            sunday, monday,
            tuesday):  # if you add wednesday it will only eval 2 points
        print(temps)
        print("min={:4.1f}, max={:4.1f}, avg={:4.1f}".format(
            min(temps), max(temps),
            sum(temps) / len(temps)))
    # chain from itertools
    all_temps = chain(sunday, monday, tuesday)
    print("All temperatures > 0", all(t > 0 for t in all_temps))
예제 #15
0
def main():
    """
    Test function
    :return:
    """
    # list with first 1 million square numbers
    m_sq = (x*x for x in range(1, 1000001))
    print(m_sq, type(m_sq))
    print('The sum of the first 1M square numbers is:', sum(m_sq))
    print('The sum of the first 1M square numbers is:',
          sum(x*x for x in range(1, 1000001)))
    # The sum of the prime numbers between 1 to 10K
    print('The sum of the prime numbers from 1 to 10K:',
          sum(x for x in range(1, 10001) if is_prime(x)))
예제 #16
0
def main():
    """
    Test function
    :return:
    list with first 1 million square numbers
    """
    m_sq = (x * x for x in range(1, 1000001))
    print(m_sq, type(m_sq))
    print('The sum of the first 1M square numbers is', sum(m_sq))
    print('The sum of the first 1M square numbers is',
          sum(x * x for x in range(1, 1000001)))
    # The sum of the prime numbers between 1 to 10K; add a filter which is prime
    print('The sum of the prime numbers between 1 to 10K;',
          sum(x for x in range(1, 10001) if is_prime(x)))
예제 #17
0
def main():
    """
    Test function
    :return:
    """
    thousand_primes = islice((x for x in count() if is_prime(x)), 1000)
    print(thousand_primes, type(thousand_primes))
    print("List of frist 1K prime numbers: ", list(thousand_primes))
    # Note: if you need to use the object again, you need to re-generate it.
    thousand_primes = islice((x for x in count() if is_prime(x)), 1000)
    print("Sum of frist 1K prime numbers: ", sum(thousand_primes))
    # Other built-ins use with itertools: any (like "or"), all (like "and")
    print(any([False, False, True]))
    print(all([False, False, True]))
    print("Are there prime numbers between 1328 and 1362: ",
          any(is_prime(x) for x in range(1328, 1362)))
    # Check if all names are in "Title" format
    names = ["London", "New York", "ogden"]
    print(all(name == name.title() for name in names))
    # Another built-in: zip()
    sunday = [2, 2, 5, 7, 9, 10, 9, 6, 4, 4]
    monday = [12, 14, 14, 15, 15, 16, 15, 13, 10, 9]
    tuesday = [13, 14, 15, 15, 16, 17, 16, 16, 12, 12]
    wednesday = [12, 12]
    for item in zip(monday, tuesday):
        print(item)
    for mon, tue in zip(monday, tuesday):  #unpacked the tuple
        print("Avg: ", (mon + tue) / 2)
    # Calculate the min, max and avg of all points
    for temp in zip(sunday, monday, tuesday):
        print("Min={:7.3f}   Max={:7.3f}   Avg={:7.3f}".format(
            min(temp), max(temp),
            sum(temp) / len(temp)))
    # chaining
    all_temps = chain(sunday, monday, tuesday)
    print("All temps > 0: ", all(t > 0 for t in all_temps))
예제 #18
0
def main():
    """
    test function for words library
    :return: nohing
    """

    # list with first 1 million square numbers
    m_sq = (x * x for x in range(1, 1000001))
    print(m_sq, type(m_sq))
    print("The sum of the first 1M squared numbers is: ", sum(m_sq))
    print("The sum of the first 1M squared numbers is: ",
          sum(x * x for x in range(1, 1000000)))

    # the sum of the prime numbers between 1 to 10K
    print("The sum of the first 10K squared numbers is: ",
          sum(x for x in range(1, 10001) if is_prime(x)))
예제 #19
0
def main():
    """
    Test function
    :return: 
    """
    m_squares = (x*x for x in range(1, 1000001)) # list with first one million square numbers
    print(m_squares, type(m_squares))
    # -> <generator object main.<locals>.<genexpr> at 0x0000018268C39518> <class 'generator'>
    # sum: sum all items of a collections
    print('The sum of the first million numbers is:', sum(m_squares))
    # -> The sum of the first million numbers is: 333333833333500000
    # or do all in one line
    print('The sum of the first million numbers is:', sum(x*x for x in range(1, 1000001)))
    # -> The sum of the first million numbers is: 333333833333500000

    # is_prime is in list_comprehensions lets call it so we can use it here, how to import
    # from list_comprehensions import is_prime

    # the sum of the prime numbers between 1 to 10K : we are using is_prime as a filter
    print('The sum of the prime numbers from 1 to 10K is:', sum(x for x in range(1, 10001) if is_prime(x)))