You are on page 1of 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