-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay 21: Generics.cs
101 lines (81 loc) · 2.37 KB
/
Day 21: Generics.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*
Objective:
Today we're discussing Generics; be aware that not all languages support this construct,
so fewer languages are enabled for this challenge.
Check out the Tutorial tab for learning materials and an instructional video!
https://www.hackerrank.com/challenges/30-generics/tutorial
Task:
Write a single generic function named printArray;
this function must take an array of generic elements as a parameter (the exception to this is C++,
which takes a vector). The locked Solution class in your editor tests your function.
Note: You must use generics to solve this challenge. Do not write overloaded functions.
Input Format:
The locked Solution class in your editor will pass different types of arrays to your printArray function.
Constraints:
You must have exactly 1 function named printArray.
Output Format:
Your printArray function should print each element of its generic array parameter on a new line.
# Provided by HackerRank:
using System;
class Printer
{
//
* Name: PrintArray
* Print each element of the generic array on a new line. Do not return anything.
* @param A generic array
/
// Write your code here
static void Main(string[] args)
{
int n = Convert.ToInt32(Console.ReadLine());
int[] intArray = new int[n];
for (int i = 0; i < n; i++)
{
intArray[i] = Convert.ToInt32(Console.ReadLine());
}
n = Convert.ToInt32(Console.ReadLine());
string[] stringArray = new string[n];
for (int i = 0; i < n; i++)
{
stringArray[i] = Console.ReadLine();
}
PrintArray<Int32>(intArray);
PrintArray<String>(stringArray);
}
}
*/
// Solution:
using System;
class Printer
{
/**
* Name: PrintArray
* Print each element of the generic array on a new line. Do not return anything.
* @param A generic array
**/
// Write your code here
static void PrintArray<T>(T[] array)
{
foreach (T element in array)
{
Console.WriteLine(element);
}
}
static void Main(string[] args)
{
int n = Convert.ToInt32(Console.ReadLine());
int[] intArray = new int[n];
for (int i = 0; i < n; i++)
{
intArray[i] = Convert.ToInt32(Console.ReadLine());
}
n = Convert.ToInt32(Console.ReadLine());
string[] stringArray = new string[n];
for (int i = 0; i < n; i++)
{
stringArray[i] = Console.ReadLine();
}
PrintArray<Int32>(intArray);
PrintArray<String>(stringArray);
}
}