Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Identify and output to the screen records for the 8 foods that have the lowest Sugar to TotalFat ratio that is not zero. Accomplish this

Identify and output to the screen records for the 8 foods that have the lowest Sugar to TotalFat ratio that is not zero. Accomplish this by using R piping. Hint: mutate(), arrange(), head().

Proj06.py (THIS ONE CAN BE MODIFIED)

import cards

def less_than(c1,c2): '''Return True if c1 is smaller in rank, True if ranks are equal and c1 has a 'smaller' suit False otherwise''' if c1.rank() < c2.rank(): return True elif c1.rank() == c2.rank() and c1.suit() < c2.suit(): return True return False def min_in_list(L): '''Return the index of the mininmum card in L''' min_card = L[0] # first card min_index = 0 for i,c in enumerate(L): if less_than(c,min_card): # found a smaller card, c min_card = c min_index = i return min_index def cannonical(H): ''' Selection Sort: find smallest and swap with first in H, then find second smallest (smallest of rest) and swap with second in H, and so on...''' for i,c in enumerate(H): # get smallest of rest; +i to account for indexing within slice min_index = min_in_list(H[i:]) + i H[i], H[min_index] = H[min_index], c # swap return H

def flush_7(H): '''Return a list of 5 cards forming a flush, if at least 5 of 7 cards form a flush in H, a list of 7 cards, False otherwise.''' pass

def straight_7(H): '''Return a list of 5 cards forming a straight, if at least 5 of 7 cards form a straight in H, a list of 7 cards, False otherwise.''' pass def straight_flush_7(H): '''Return a list of 5 cards forming a straight flush, if at least 5 of 7 cards form a straight flush in H, a list of 7 cards, False otherwise.''' pass

def four_7(H): '''Return a list of 4 cards with the same rank, if 4 of the 7 cards have the same rank in H, a list of 7 cards, False otherwise.''' pass

def three_7(H): '''Return a list of 3 cards with the same rank, if 3 of the 7 cards have the same rank in H, a list of 7 cards, False otherwise. You may assume that four_7(H) is False.''' pass def two_pair_7(H): '''Return a list of 4 cards that form 2 pairs, if there exist two pairs in H, a list of 7 cards, False otherwise. You may assume that four_7(H) and three_7(H) are both False.''' pass

def one_pair_7(H): '''Return a list of 2 cards that form a pair, if there exists exactly one pair in H, a list of 7 cards, False otherwise. You may assume that four_7(H), three_7(H) and two_pair(H) are False.''' pass

def full_house_7(H): '''Return a list of 5 cards forming a full house, if 5 of the 7 cards form a full house in H, a list of 7 cards, False otherwise. You may assume that four_7(H) is False.''' pass

def main(): D = cards.Deck() D.shuffle() while True: # create community cards # create Player 1 hand # create Player 2 hand print("-"*40) print("Let's play poker! ") print("Community cards:",community_list) print("Player 1:",hand_1_list) print("Player 2:",hand_2_list) print()

if name == "main": main()

cards.py( CANNOT BE MODIFIED)

import random

""" NOTE: If your terminal can't decode the unicode card suit symbols, set up the bash terminal language environment to "en_US.utf8", like this --

export LANG=en_US.utf8

You might also want to put this in your ~/.bashrc to make the change permanent accross the sessions.

For tcsh or zsh, there are similar methods exist, please look them up.

"""

class Card( object ): """ Model a playing card. """

# Rank is an int (1-13), where aces are 1 and kings are 13. # Suit is an int (1-4), where clubs are 1 and spades are 4. # Value is an int (1-10), where aces are 1 and face cards are 10.

# List to map int rank to printable character (index 0 used for no rank) rank_list = ['x','A','2','3','4','5','6','7','8','9','10','J','Q','K']

# List to map int suit to printable character (index 0 used for no suit) # 1 is clubs, 2 is diamonds, 3 is hearts, and 4 is spades # suit_list = ['x','c','d','h','s'] # for systems that cannot print Unicode symbols suit_list = ['x','\u2663','\u2666','\u2665','\u2660'] def init( self, rank=0, suit=0 ): """ Initialize card to specified rank (1-13) and suit (1-4). """ self.__rank = 0 self.__suit = 0 self.__face_up = None # Verify that rank and suit are ints and that they are within # range (1-13 and 1-4), then update instance variables if valid. if type(rank) == int and type(suit) == int: if rank in range(1,14) and suit in range(1,5): self.__rank = rank self.__suit = suit self.__face_up = True def rank( self ): """ Return card's rank (1-13). """ return self.__rank

def value( self ): """ Return card's value (1 for aces, 2-9, 10 for face cards). """ # Use ternary expression to determine value. return self.rank if self.rank < 10 else 10

def suit( self ): """ Return card's suit (1-4). """ return self.__suit def is_face_up( self ): """ Returns True if card is facing up.""" return self.__face_up def flip_card( self ): """ Flips card between face-up and face-down""" self.face_up = not self.face_up

def str( self ): """ Convert card into a string (usually for printing). """ # Use rank to index into rank_list; use suit to index into suit_list. if self.__face_up: return "{}{}".format( (self.rank_list)[self.__rank], \ (self.suit_list)[self.__suit] ) else: return "{}{}".format( "X", "X")

def repr( self ): """ Convert card into a string for use in the shell. """ return self.str() def eq( self, other ): """ Return True, if Cards of equal rank and suit; False, otherwise. """ if not isinstance(other, Card): return False return self.rank() == other.rank() and self.suit() == other.suit() class Deck( object ): """ Model a deck of 52 playing cards. """

# Implement the deck as a list of cards. The last card in the list is # defined to be at the top of the deck.

def init( self ): """ Initialize deck--Ace of clubs on bottom, King of spades on top. """ self.__deck = [Card(r,s) for s in range(1,5) for r in range(1,14)]

def shuffle( self ): """ Shuffle deck using shuffle method in random module. """ random.shuffle(self.__deck)

def deal( self ): """ Return top card from deck (return None if deck empty). """ # Use ternary expression to guard against empty deck. return self.deck.pop() if len(self.deck) else None

def is_empty( self ): """ Return True if deck is empty; False, otherwise """ return len(self.__deck) == 0

def len( self ): """ Return number of cards remaining in deck. """ return len(self.__deck) def str( self ): """ Return string representing deck (usually for printing). """ return ", ".join([str(card) for card in self.__deck]) def repr( self ): """ Return string representing deck (for use in shell). """ return self.str()

def display( self, cols=13 ): """ Column-oriented display of deck. """ for index, card in enumerate(self.__deck): if index%cols == 0: print() print("{:3s} ".format(str(card)), end="" ) print() print()

cardsDemo.py (CANNOT BE MODIFIED)

import cards ''' The basic process is this: 1) A Deck instance is created, which is filled (automatically) with 52 Card instances 2) You can deal those cards out of the deck into hands, each hand a list of cards 3) You then manipulate cards as you add/remove them from a hand ''' my_deck = cards.Deck() print("======messy print a deck=====") print(my_deck)

print("======pretty print a deck=====") my_deck.display()

my_deck.shuffle() print("======shuffled deck=====") my_deck.display()

a_card = my_deck.deal() print("Dealt card is:",a_card) print('How many cards left:',len(my_deck))

print("Is the deck empty?",my_deck.is_empty())

# deal some hands and print hand1_list=[] hand2_list=[] for i in range(5): hand1_list.append(my_deck.deal()) hand2_list.append(my_deck.deal()) print(" Hand 1:", hand1_list) print("Hand 2:", hand2_list) print()

# take the last card dealt out of each hand last_card_hand1 = hand1_list.pop() last_card_hand2 = hand2_list.pop() print("Hand1 threw down",last_card_hand1, ", Hand2 threw down", last_card_hand2) print("Hands are now:",hand1_list, hand2_list)

# check the compares if last_card_hand1 == last_card_hand2: print(last_card_hand1, last_card_hand2, "of equal rank") elif last_card_hand1.rank() > last_card_hand2.rank(): print(last_card_hand1.rank(), "of higher rank than",last_card_hand2.rank()) else: print(last_card_hand2.rank(), "of higher rank than",last_card_hand1.rank())

if last_card_hand1.value() == last_card_hand2.value(): print(last_card_hand1, last_card_hand2, "of equal value") elif last_card_hand1.value() > last_card_hand2.value(): print(last_card_hand1, "of higher value than",last_card_hand2) else: print(last_card_hand2, "of higher value than",last_card_hand1)

if last_card_hand1.suit() == last_card_hand2.suit(): print(last_card_hand1,'of equal suit with',last_card_hand2) else: print(last_card_hand1,'of different suit than',last_card_hand2)

# a foundation, a list of lists. 4 columns in this example foundation_list = [[],[],[],[]] column = 0 while not my_deck.is_empty(): foundation_list[column].append(my_deck.deal()) column += 1 if column % 4 == 0: column = 0 for i in range(4): print("foundation",i,foundation_list[i])

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

Operating Systems Internals and Design Principles

Authors: William Stallings

8th edition

133805913, 978-0133805918

More Books

Students also viewed these Programming questions