1 Replies - 1619 Views - Last Post: 14 December 2012 - 11:39 PM Rate Topic: -----

#1 Chioma18  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 14-December 12

Debug Script

Posted 14 December 2012 - 02:24 PM

Questions:

pad_names:
(list of str, int) -> int
The first parameter is a list of names of various lengths. The second parameter is a minimum length. For any name whose length is less than the minimum, replace that item of the list with a new string containing the name with space(s) added to the right-hand side to achieve the minimum length. For example, if the list contains the name "Sue" and the minimum length is 5, then that item would be replaced with the name padded with 2 spaces: "Sue ". Any names that are already the minimum length or longer are unchanged. Return the number of names that are longer than the minimum length.


get_most_popular:
(list of str, dict of {str: list of str}) -> list of str
The first parameter is a list of movies that are currently in stock at a movie store. The second parameter is a dictionary where each key is the name of a customer and each value is a list of that customer's favourite movies ordered from most to least favourite. It is possible for a customer to have a favourite movie that is not in stock at the store. Return a list of all in-stock movies that are the first- or second-favourite movie of at least one customer. The movies in the returned list must be in the same order as in the first parameter and the returned list must not contain duplicates. The parameters are not changed by the function.


daily_percent_by_district:
(list of list of str, str) -> list of int
A small business owner asks each customer for their postal code in order to find out where they live. A postal code is 6 characters (e.g., 'H0H0H0') and the first three characters represent a district.

The first parameter is a list of list of postal codes, where each inner list contains the postal codes of customers for one business day. The second parameter is a three-letter code for a district. Return a list where each element is the percentage of customers from that district for the business day at the corresponding position in the list of list of postal codes.

Each percentage is to be an integer between 0 and 100. Floating point values are to be truncated to the integer below. For example, if on a particular business day, there were three postal codes and two of them were from the district of interest, the corresponding element in the returned list would be 66.
Precondition: each inner list in the list of list of postal codes has at least one element


import math
def pad_names(words, value_minimum):
    new_list = []
    count = 0
    for names in words:
        if len(names) < value_minimum:
            count += 1 
            new_name = names + ((value_minimum - len(names)) * ' ')
            new_list.append(new_name)
        else:
            count += 0
            new_name = names
            new_list.append(new_name)
    return count  

        
             
def get_most_popular(movies_stock, favorite_movies):
    fav_list = []
    fav_movies = list(favorite_movies.values()) # returns the values as a list      
    for movies in fav_movies:
        for movie in movies[:2]:
            if movie in movies_stock:
                fav_list.append(movie)
                break 
    return fav_list 

def daily_percent_by_district(postal_code, district):
    count_code = []
    count = 0
    for codes in postal_code:
        for letters in codes:
            if letters[:3] == district:
                count += 1 
            else:
                count += 0
        letter_code = (count/len(codes)) * 100
        L = math.floor(letter_code)
        count_code.append(L)
    return count_code 

 


Hello,
I don't know what the problem is, but each function fails for complex test cases.
Especially the last function, it doesn't work for the following test cases:
[['A1A2B2'], ['H0H0H0', 'A1A2B2']],'A1A'
[['A1A2B2'], ['HOHOHO'], ['A1A5A4', 'H0H0H0', 'A1A2B2']], 'A1A'

I don't know what the problem is. Please help!. Thank you

Is This A Good Question/Topic? 0
  • +

Replies To: Debug Script

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4421
  • View blog
  • Posts: 12,286
  • Joined: 18-April 07

Re: Debug Script

Posted 14 December 2012 - 11:39 PM

Put your count = 0 inside your first for statement. You have to reset count for each element of the list. Since you are not resetting it each time, it is being carried over.

Also, you don't need the else: count += 0. Just don't have the else. If it matches, add 1, otherwise don't do anything. That statement of adding 0 is useless.

Once you put the count statement inside the first for statement, it should work fine.

def daily_percent_by_district(postal_code, district):
    count_code = []
    for codes in postal_code:
        count = 0
        for letters in codes:
            if letters[:3] == district:
                count += 1 
        letter_code = (count/len(codes)) * 100
        L = math.floor(letter_code)
        count_code.append(L)
    return count_code 



Enjoy! :)

This post has been edited by Martyr2: 14 December 2012 - 11:40 PM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1