查看: 3621|回复: 124
|
高手进来,看看一个interview的问题。做对有赏~
[复制链接]
|
|
本帖最后由 martinng 于 22-10-2018 10:33 PM 编辑
public class Student
{
public string name;
public char sex;
}
public static Student[] studentList = new Student[] {
new Student() { name = "Martin", sex = 'M' },
new Student() { name = "Jenny", sex = 'F' },
new Student() { name = "Wendy", sex = 'F' },
new Student() { name = "Siti", sex = 'F' },
new Student() { name = "Thomas", sex = 'M' },
new Student() { name = "Siva", sex = 'M' },
new Student() { name = "Mun Hui", sex = 'F' },
new Student() { name = "Richard", sex = 'M' },
new Student() { name = "Kumar", sex = 'M' },
new Student() { name = "Isah", sex = 'F' },
new Student() { name = "Samson", sex = 'M' },
new Student() { name = "JiaJia", sex = 'F' },
};
Question: Write code to print studentList in format ({name} - {sex}), boys go first followed by girls. Sorting among boys / girls is not neccessary.
Rules:
- Any languages even pseudocode can be used.
- Language specific features are NOT allowed, such as C/C like languages ternary operator: (x==y)?true:false.
- ONLY one for/while loop can be used, but cannot be nested.
- ONLY integer and boolean variables can be used.
- ONLY standard output functions such as printf/cout/echo/console.Write and others similar can be used.
- The studentList is NOT allowed to be extended or duplicated.
|
|
|
|
|
|
|
|
楼主 |
发表于 22-10-2018 10:44 PM
|
显示全部楼层
这是朋友去新加坡软件公司面试时的题目。他抄给我看,我以为很容易就随手写了大概5~6行(包括for loop)。看了答案后才发觉原来可以更少行!
if (xxx) print yyy <--- 算1行
if (xxx) {
print yyy <----也算1行
}
if (xxx) {
print yyy <----算2行
print zzz
}
if (xxx) yyy, zzz <--- 也算2行
|
|
|
|
|
|
|
|
楼主 |
发表于 22-10-2018 10:50 PM
|
显示全部楼层
目前有1个人做到一行for loop,一行if print(应该是最简化了):
- for (int i = 0; i < 2 * n; i++)
- {
- if (studentList[i % n].sex == (i < n ? 'M' : 'F'))
- {
- Console.WriteLine(string.Format("{0} - {1}", studentList[i % n].name, studentList[i % n].sex));
- }
- }
复制代码
但却犯规了,因为用到C 的ternary operator,所以DQ了。
|
|
|
|
|
|
|
|
楼主 |
发表于 22-10-2018 10:53 PM
|
显示全部楼层
另外一个也是写到很简:
- foreach(var student in studentList.OrderByDescending(x => x.sex)){ ... string.format("{0}-{1}",student.name,student.sex) }
复制代码
但也是DQ了。因为call到standard output以外的functions/methods/calls。 |
|
|
|
|
|
|
|
楼主 |
发表于 22-10-2018 11:03 PM
|
显示全部楼层
我看了官方答案,只有4行而已。而且是pseudo code的写法,变成任何language都是同样的写法。我以为没有更好了,却有一个中国programmer把这个官方答案化简到两行而已一行for loop + 一行if print!
在此玩个游戏,我悬赏RM50,如果有人可以满足所有条件而且只有一行for loop + 一行if print而已~ |
|
|
|
|
|
|
|
发表于 23-10-2018 08:50 AM
|
显示全部楼层
本帖最后由 yan13 于 23-10-2018 10:23 AM 编辑
不懂符合條件嗎- int n = studentList.Length;
- int i = 0;
- int t = System.Convert.ToInt32('M');
-
- do
- {
- if (System.Convert.ToInt32(studentList[i % n].sex) == t)
- {
- Console.WriteLine(string.Format("{0} - {1}", studentList[i % n].name, studentList[i % n].sex));
- }
- if (i == n-1)
- {
- t = System.Convert.ToInt32('F');
- }
- i++;
- } while (i < n*2);
复制代码
|
|
|
|
|
|
|
|
发表于 23-10-2018 08:54 AM
|
显示全部楼层
本帖最后由 yan13 于 23-10-2018 10:33 AM 编辑
想用2個string,1個收"M",1個收"F",after loop, concat both string then print.應該好過上面的,不過不能用string發現到和第一個的答案一樣思路,想刪除帖子但不會.就算了
|
|
|
|
|
|
|
|
楼主 |
发表于 23-10-2018 11:47 AM
|
显示全部楼层
本帖最后由 martinng 于 23-10-2018 11:48 AM 编辑
yan13, 是犯了一条规:
- ONLY standard output functions such as printf/cout/echo/console.Write and others similar can be used.
之前就是有人用Collection.sort(),然后print studentList,不懂是否真的可以run到,但因为违反只能用standard output function/method/call 而已,所以DQ了。
System.Convert.ToInt32()<--- 这个是standard output之外的method了。 |
|
|
|
|
|
|
|
楼主 |
发表于 23-10-2018 11:50 AM
|
显示全部楼层
我觉得这家公司要出这种鬼问题,是因为要programmer写出portable to every system的代码,尽量减少依赖。 |
|
|
|
|
|
|
|
楼主 |
发表于 23-10-2018 11:54 AM
|
显示全部楼层
有人试过了,但DQ了,哈哈哈。因为额外String variable是不允许的。
|
|
|
|
|
|
|
|
发表于 23-10-2018 02:03 PM
|
显示全部楼层
其實看到第一個回答的思路我就不太想寫了.覺得一樣,像是在抄他的.然後又不會刪貼,所以算了.不過既然都開始了,做完它吧- int n = studentList.Length;
- int i = 0;
-
- do
- {
- if (((studentList[i % n].sex) == 'M' && i < n) || ((studentList[i % n].sex) == 'F' && i > n))
- {
- Console.WriteLine(string.Format("{0} - {1}", studentList[i % n].name, studentList[i % n].sex));
- }
- i++;
- } while (i < n*2);
复制代码
|
|
|
|
|
|
|
|
楼主 |
发表于 23-10-2018 02:37 PM
|
显示全部楼层
本帖最后由 martinng 于 23-10-2018 03:21 PM 编辑
n*2 和 mod 看来是必须的。
你这个code可以再简化到:
- for (int i = 0; i < studentList.Length * 2; i ++) {
- if (((studentList[i % studentList.Length].sex) == 'M' && i < studentList.Length) || ((studentList[i % studentList.Length].sex) == 'F' && i >= studentList.Length))
- {
- Console.WriteLine(string.Format("{0} - {1}", studentList[i % studentList.Length].name, studentList[i % studentList.Length].sex));
- }
- }
复制代码
|
|
|
|
|
|
|
|
楼主 |
发表于 23-10-2018 03:24 PM
|
显示全部楼层
其实我也觉得你是做到了一行loop+一行if print了。但还有更elegant的写法,试试看~ |
|
|
|
|
|
|
|
发表于 23-10-2018 10:48 PM
|
显示全部楼层
算了吧.我一向不太嚮往以最短的code來完成同樣的東西.
|
|
|
|
|
|
|
|
发表于 24-10-2018 04:05 AM
|
显示全部楼层
不知道这个PSEUDOCODE如何?我有用到ABS() 即:
ABS() returns the absolute value of a variable. The result of the function has the same type as its argument, which can be any numerical type.
看起来yan13已经是最好的了。
- FOR COUNT=-11 TO 11
- IF (STUDENTLIST(ABS(COUNT)).SEX='M' AND COUNT<=0) OR (STUDENTLIST(ABS(COUNT)).SEX='F' AND COUNT=>0) THEN
- PRINT STUDENTLIST(ABS(COUNT)).NAME & " - " & STUDENTLIST(ABS(COUNT)).SEX
- NEXT
复制代码
|
|
|
|
|
|
|
|
楼主 |
发表于 24-10-2018 04:16 AM
|
显示全部楼层
褐眼睛,good try。可惜ABS()是standard output call之外了。
yan13的code真的还可以再简化(IF statement) |
|
|
|
|
|
|
|
发表于 24-10-2018 06:14 AM
|
显示全部楼层
尝试改良了yan13的部分,不过还是一样长。
- FOR COUNT=0 TO (STUDENTLIST.LENGTH X 2) - 1
- IF (STUDENTLIST[COUNT MOD 12].SEX='M' AND COUNT=COUNT MOD 12) OR (STUDENTLIST[COUNT MOD 12].SEX='F' AND COUNT>COUNT MOD 12) THEN
- PRINT STUDENTLIST[COUNT MOD 12].NAME & " - " & STUDENTLIST[COUNT MOD 12].SEX
- ENDIF
- NEXT
复制代码
|
|
|
|
|
|
|
|
发表于 24-10-2018 07:41 AM
来自手机
|
显示全部楼层
本帖最后由 aquamax 于 24-10-2018 07:45 AM 编辑
褐眼睛 发表于 24-10-2018 06:14 AM
尝试改良了yan13的部分,不过还是一样长。
把count > 12先测。。
if count < 12 and M
...Print student list[count mod 12]
Else F
... |
|
|
|
|
|
|
|
楼主 |
发表于 24-10-2018 01:57 PM
|
显示全部楼层
yan13的code是用最少行,这个是肯定了。只是他的if (。。。)还可以再更简化。给点贴士:需要用到一点数学。。。 |
|
|
|
|
|
|
|
发表于 24-10-2018 07:55 PM
|
显示全部楼层
楼主说有中国高手只需一行loop和一行if print就搞定了。
你列出的这个方法超过一行了。
|
|
|
|
|
|
|
| |
本周最热论坛帖子
|