Question

In: Computer Science

Yesterday you found some shoes in the back of your closet. Each shoe is described by...

Yesterday you found some shoes in the back of your closet. Each shoe is described by two values:

  • type indicates if it's a left or a right shoe;
  • size is the size of the shoe.

Your task is to check whether it is possible to pair the shoes you found in such a way that each pair consists of a right and a left shoe of an equal size.

Example

  • For

    shoes = [[0, 21], 
             [1, 23], 
             [1, 21], 
             [0, 23]]
    

    the output should be
    pairOfShoes(shoes) = true;

  • For

    shoes = [[0, 21], 
             [1, 23], 
             [1, 21], 
             [1, 23]]
    

    the output should be
    pairOfShoes(shoes) = false.

Input/Output

  • [execution time limit] 4 seconds (py3)

  • [input] array.array.integer shoes

    Array of shoes. Each shoe is given in the format [type, size], where type is either 0 or 1 for left and right respectively, and size is a positive integer.

    Guaranteed constraints:
    1 ≤ shoes.length ≤ 200,
    1 ≤ shoes[i][1] ≤ 100.

  • [output] boolean

    true if it is possible to pair the shoes, false otherwise.

Input:

 

shoes: [[0,21], [1,23], [1,21], [0,23]]

Expected Output:

true
 

shoes: [[0,21], [1,23], [1,21], [1,23]]

Expected Output:

false

Use Python

def pairOfShoes(shoes):

Solutions

Expert Solution

The answer to the above problem is as follows:

  • first the function will be defined.
  • Then inside the function, define an empty array of 100 rows and 2 columns, each array will act as a hash table that stores the shoes data, each row index of hash table means the shoe size, and if column 0 has value 1, it means left shoe is there for this size and if column 1 has value 1, it means right shoe is there for this size.
  • the hash table is created and then further operations are carried out as mentioned in the comments of the code.

PYTHON CODE-

#define the method that takes the shoes array as input parameters
def pairOfShoes(shoes):
#define an empty array of 100 rows and 2 columns,
#each array will act as a hash table that stores the shoes data
#each row index of hash table means the shoe size
#and if column 0 has value 1, it means left shoe is there for this size
#and if column 1 has value 1, it means right shoe is there for this size
  
#create a hash table to store the data of shoes in a way define above
#row size is 101, because we have shoe size from 1 to 100, and arrays are 0 indexed
a = [[0 for x in range(2)] for y in range(101)]
  
#iterate over the shoes array
for i in range(len(shoes)):
#for left shoe, have value 1 in column 0 in the hash table for that shoe
if(shoes[i][0] == 0):
a[shoes[i][1]][0] = 1
#for right shoe, have value 1 in column 1 in the hash table for that shoe
elif(shoes[i][0] == 1):
a[shoes[i][1]][1] = 1
  
#initially flag is True
flag = True
  
#iterate over the hash table array
for i in range(1,len(a)):
#if at that row, there is atleast 1 column with value 1, it means there is one shoe that might be left or right
if(a[i][0]==1 or a[i][1]==1):
#check if both left and right are not there, meaning either column 0 or 1 has the value which is not 1
if(a[i][0]!=1 or a[i][1]!=1):
#it means the pair is not there
flag = False #set flag False
break #break the loop
  
#return the result
return flag
  
#demo values and function call to test the function and display the result
shoes = [[0,21], [1,23], [1,21], [0,23],[0,18],[1,19]]
print(pairOfShoes(shoes))

IMAGE OF CODE and OUTPUT-

If this answer helps, please give an up vote and feel free to comment for any query.


Related Solutions

You are the manager of a shoe producer. Your company specializes in basketball shoes and a...
You are the manager of a shoe producer. Your company specializes in basketball shoes and a cleaning product for basketball shoes. While the shoes bring in more revenue ($500,000 per year), the cleaning product is a strong seller as well ($150,000 of revenue per year). You are considering a 2% decrease in the price of your company's basketball shoes. Assume that the shoes have an own price elasticity of demand of -1.8 and the shoes and cleaning product have a...
You are the manager of a shoe producer. Your company specializes in basketball shoes and a...
You are the manager of a shoe producer. Your company specializes in basketball shoes and a cleaning product for basketball shoes. While the shoes bring in more revenue ($500,000 per year), the cleaning product is a strong seller as well ($150,000 of revenue per year). You are considering a 2% decrease in the price of your company's basketball shoes. Assume that the shoes have an own price elasticity of demand of -1.8 and the shoes and cleaning product have a...
Your company is considering buying back some of its stock. You are assigned the task of...
Your company is considering buying back some of its stock. You are assigned the task of correctly assessing the value of the company so that the company can make a rational decision. Select 4 ratios that you believe would provide the best insight into the condition of the company and provide a rationale as to why you feel that they would be most relevant. Current Ratio                                                     =                                                  Current assets/current liabilities Acid-test Ratio    =                             Cash+accounts recieveable/current liabilities Return on equity...
Your friend texted you a question. You did some research and found that a Minifig is...
Your friend texted you a question. You did some research and found that a Minifig is a package containing one figurine. You also found out that, for every 60 packs produced, 4 of those are Chip and 4 are Dale. A) What is the probability of opening one box and getting Chip? B) What is the probability of opening one box and getting Chip OR Dale? C) What is the probability of opening two boxes and specifically getting Chip in...
Sam's Shoes has problems with its best-selling shoe—the FastShoe. Sam, the owner, tells you that he...
Sam's Shoes has problems with its best-selling shoe—the FastShoe. Sam, the owner, tells you that he always seems to have too many or too few of the FastShoe. He has hired you to help determine how much and when to order. At the same time, the company is considering quotes from 2 different suppliers, and you will help compare suppliers. You estimated the following information from the detailed records that Sam kept on the shoe. You calculated the standard deviation...
Your friend is talking to you about one of their investments. Yesterday, your friend received a...
Your friend is talking to you about one of their investments. Yesterday, your friend received a cheque from the investment for $6,000. According to the company’s annual report and forecast, the company expects the amount of the cash flow to increase or grow at 15 percent for the next 5 years and then to grow at 2 percent forever. 1. If you require a 15% return on your investment, what is the value of this investment today? 2. If interest...
Yesterday, you entered into a futures contract to buy €62,500 at $1.50 per €. Your initial...
Yesterday, you entered into a futures contract to buy €62,500 at $1.50 per €. Your initial performance bond is $1,500 and your maintenance level is $500. At what settle price will you get a demand for additional funds to be posted?
Your new baby was born yesterday. To save for her education, you decide to invest in...
Your new baby was born yesterday. To save for her education, you decide to invest in a 529 plan and will make QUARTERLY contributions until your child enters the great UNLV when she turns 18. That is, you will save for the next 17 years (Or should it be 18 years? Think about it), and the contribution will be made at the END of each quarter. You expect that the 529 plan will return 8.5% per year with quarterly compounding....
Your company is considering expanding its operations in shoe manufacturing. You owe your consultants $ 1.8...
Your company is considering expanding its operations in shoe manufacturing. You owe your consultants $ 1.8 million for a​ report, but you are not sure their analysis makes sense. Before you spend the $ 21 million on new equipment needed for this​ project, you have to look it over and give it your​ opinion. You open the report and find the following estimates​ (in millions of​ dollars): 1 2 . . . 9 10 Sales revenue 32,000 32,000 32,000 32,000...
It is 2026 and you are back in your home state, where you work as chief...
It is 2026 and you are back in your home state, where you work as chief advisor to a state legislator. (Taking PSC 107 made you an expert in legislative matters – it turns out that policymaking is very similar to elections with policy-motivated candidates.) All legislators are policy motivated. This means that an arbitrary legislator i, with ideal point xi, gets utility ?|x?xi| if policy x is approved. Your boss, a moderate legislator with ideal point xB = 0.3,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT