0% found this document useful (0 votes)
44 views

Untitled

The document provides C# code that translates Java code for determining if a buyer wins a promotion based on their shopping cart contents matching promotion codes. The C# code defines a Foo class with an IsBuyerWinner method that takes code and shopping cart lists as parameters. It converts the lists to arrays, iterates through to check for matches based on order while incrementing indexes, and returns 1 if all codes are matched or 0 otherwise.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views

Untitled

The document provides C# code that translates Java code for determining if a buyer wins a promotion based on their shopping cart contents matching promotion codes. The C# code defines a Foo class with an IsBuyerWinner method that takes code and shopping cart lists as parameters. It converts the lists to arrays, iterates through to check for matches based on order while incrementing indexes, and returns 1 if all codes are matched or 0 otherwise.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

C# TRANSLATION FOR THE JAVA CODE ON SITE:

private static int FreshPromotion(string[][] codeList, string[] shoppingCart)


{
// Start at 0 index for both the code list and shopping cart.
int cartIdx = 0, codeIdx = 0;
while (cartIdx < shoppingCart.Length && codeIdx < codeList.Length)
{
string cur = shoppingCart[cartIdx];
// If the first fruit of the codeList is anything or if it matches the
current fruit at the cart idx.
if ((codeList[codeIdx][0].Equals("anything") || codeList[codeIdx]
[0].Equals(cur)) && HasOrder(shoppingCart, cartIdx, codeList[codeIdx]))
{
cartIdx += codeList[codeIdx++].Length;
}
else
{
cartIdx++;
}
}
// If the all the codeList is present then return 1, else 0.
return codeIdx == codeList.Length ? 1 : 0;
}

private static bool HasOrder(string[] shoppingCart, int idx, string[] order)


{
// Loop through the codeList to check if the fruits are in order.
foreach (string s in order)
{
if (idx < shoppingCart.Length && (s.Equals("anything") ||
shoppingCart[idx].Equals(s)))
{
idx++;
}
else
{
return false;
}
}
return true;
}

using System;
using System.Collections.Generic;
public class Foo
{
/*
* Complete the &#39;IsBuyerWinner&#39; function below.
*
* The function is expected to return an Integer.
* The function accepts following parameters:
* 1. List (STRING_ARRAY) - codeList
* 2. List (STRING_ARRAY) - shoppingCart
*/
public static int IsBuyerWinner(List&lt;string&gt; codeList, List&lt;string&gt;
shoppingCart)
{
}
}
public class Solution
{
public static void Main(string[] args)
{
int codeListCount = Convert.ToInt32(Console.ReadLine().Trim());
List&lt;string&gt; codeList = new List&lt;string&gt;();
for (int i = 0; i &lt; codeListCount; i++)
{
string codeListItem = Console.ReadLine();
codeList.Add(codeListItem);
}
int shoppingCartCount = Convert.ToInt32(Console.ReadLine().Trim());
List&lt;string&gt; shoppingCart = new List&lt;string&gt;();
for (int i = 0; i &lt; shoppingCartCount; i++)
{
string shoppingCartItem = Console.ReadLine();
shoppingCart.Add(shoppingCartItem);
}
int foo = Foo.IsBuyerWinner(codeList, shoppingCart);
Console.WriteLine(foo);
}
}

POVEZANO PREKO GPT-A:

using System;
using System.Collections.Generic;

public class Foo


{
/*
* Complete the 'IsBuyerWinner' function below.
*
* The function is expected to return an Integer.
* The function accepts following parameters:
* 1. List (STRING_ARRAY) - codeList
* 2. List (STRING_ARRAY) - shoppingCart
*/
public static int IsBuyerWinner(List<string> codeList, List<string>
shoppingCart)
{
string[][] codeListArray = new string[codeList.Count][];
for (int i = 0; i < codeList.Count; i++)
{
codeListArray[i] = codeList[i].Split(' ');
}

string[] shoppingCartArray = shoppingCart.ToArray();

// Start at 0 index for both the code list and shopping cart.
int cartIdx = 0, codeIdx = 0;
while (cartIdx < shoppingCartArray.Length && codeIdx <
codeListArray.Length)
{
string cur = shoppingCartArray[cartIdx];
// If the first fruit of the codeList is anything or if it matches the
current fruit at the cart idx.
if ((codeListArray[codeIdx][0].Equals("anything") ||
codeListArray[codeIdx][0].Equals(cur)) && HasOrder(shoppingCartArray, cartIdx,
codeListArray[codeIdx]))
{
cartIdx += codeListArray[codeIdx++].Length;
}
else
{
cartIdx++;
}
}
// If the all the codeList is present then return 1, else 0.
return codeIdx == codeListArray.Length ? 1 : 0;
}

private static bool HasOrder(string[] shoppingCart, int idx, string[] order)


{
// Loop through the codeList to check if the fruits are in order.
foreach (string s in order)
{
if (idx < shoppingCart.Length && (s.Equals("anything") ||
shoppingCart[idx].Equals(s)))
{
idx++;
}
else
{
return false;
}
}
return true;
}
}

public class Solution


{
public static void Main(string[] args)
{
int codeListCount = Convert.ToInt32(Console.ReadLine().Trim());
List<string> codeList = new List<string>();
for (int i = 0; i < codeListCount; i++)
{
string codeListItem = Console.ReadLine();
codeList.Add(codeListItem);
}

int shoppingCartCount = Convert.ToInt32(Console.ReadLine().Trim());


List<string> shoppingCart = new List<string>();
for (int i = 0; i < shoppingCartCount; i++)
{
string shoppingCartItem = Console.ReadLine();
shoppingCart.Add(shoppingCartItem);
}

int result = Foo.IsBuyerWinner(codeList, shoppingCart);


Console.WriteLine(result);
}
}
KOMENTARI NA BHS:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy