26 April 2017

digitsProduct

My solution:
int digitsProduct(int product) 
{
    switch (product)
    {
        case 0:
            return 10;
        case 1:
            return 1;
        default:
            var i = 9;
            var answer = string.Empty;
            var copyOfProduct = product;
            while (product != 1 && i != 1)
            {
                if (product % i == 0)
                {
                    product /= i;
                    answer += i.ToString();
                }
                else
                    i--;
            }

            if (i == 1 && copyOfProduct == product)
                return -1;

            return i == 1 && copyOfProduct == product ? -1 : int.Parse(new string(answer.Reverse().ToArray()));
    }
}

Previous Next

Different Squares

My solution:
int differentSquares(int[][] matrix) 
{
    var two_x_two_s = new HashSet<Tuple<int, int, int, int>>();
    for (var row = 0; row < matrix.Length - 1; row++)
        for (var column = 0; column < matrix[0].Length - 1; column++)
            two_x_two_s.Add(new Tuple<int, int, int, int>(matrix[row][column], matrix[row][column + 1],
                matrix[row + 1][column], matrix[row + 1][column + 1]));

    return two_x_two_s.Count;
}

Previous Next

sumUpNumbers

My solution:
int sumUpNumbers(string inputString) 
{
    return Regex.Matches(inputString, "\\d+").Cast<Match>().Sum(match => int.Parse(match.Value));
}

Previous Next

Valid Time

My solution:
bool validTime(string time) 
{
    return Regex.IsMatch(time, "^([01]\\d|2[0-3]):([0-5]\\d)$");
}

Previous Next

longestWord

My solution:
string longestWord(string text) 
{
    var list = (from Match match in Regex.Matches(text, "[A-Za-z]+") select match.Value).ToList();
    return list.First(s => s.Length == list.Select(s1 => s1.Length).Max());
}

Previous Next

25 April 2017

Hanzi Week 3

Characters and Words:

Week 3
Lesson 9:
1. xué  2. shēng (shēng hái zi, xué sheng, nǚ shēng)  3.  (xué xí)  4. lǎo (lǎo rén)  5. shī (lǎo shī)  6. wén (wén xué, wén zǐ, zuò wén)

Lesson 10:
1.  (dà rén, dà xué, dà xué shēng)  2. xiǎo (xiǎo háir, xiǎo xué, xiǎo xué shēng, dà xiǎo)  3. duō  4. shǎo (duō shao)  5. hǎo (hǎo duō, hǎo rén)  6. huài (huài rén., huài hǎo)

Lesson 11:
1. lái  2.   3. jìn (jìn lai, jìn qu)  4. chū (chū lai, chū qu, chū shēng)  5. kāi (kāi xué)  6. guān (kāi guān)

Lesson 12:
1. cháng  2. duǎn (cháng duǎn)  3. gāo (gāo gè zi)  4. xìng (gāo xìng)  5. xīn (xīn shēng)  6. jiù (xīn jiù)

Previous
from Coursera Chinese Characters for beginner 汉字 by Peking University
Link: https://www.coursera.org/learn/hanzi/home

22 April 2017

Hanzi Week 2

Characters and Words:

Week 2
Lesson 5:
1.   2.   3. men (rén men, nǐ men, tā men)  4. zuò (gōng zuò)  5. shén  6. me (shén me)

Lesson 6:
1.  (nǚ rén) 2.  (tā men)  3.  (mā ma)  4. jiě (jiě jie)  5. mèi (mèi mei, jiě mèi)  6. nǎi (nǎi nai)

Lesson 7:
1.   2. qīn (fù qin)  3.  (bà ba)  4.  (yé ye)  5.  (mǔ qin, fù mǔ)  6.  (wǒ men)

Lesson 8:
1. ér (nǚ ér)  2.  (ér zi, gè zi, zǐ nǚ)  3. sūn (sūn zi, sūn nü)  4. hái (hái zi, nü háir)  5. míng  6.  ( míng zi)

Previous Next
from Coursera Chinese Characters for beginner 汉字 by Peking University
Link: https://www.coursera.org/learn/hanzi/home