星期一 ~ 星期日
var today = DateTime.Now;
if (today.DayOfWeek == DayOfWeek.Monday)
if (today.DayOfWeek == DayOfWeek.Tuesday)
if (today.DayOfWeek == DayOfWeek.Wednesday)
if (today.DayOfWeek == DayOfWeek.Thursday)
if (today.DayOfWeek == DayOfWeek.Friday)
if (today.DayOfWeek == DayOfWeek.Saturday)
if (today.DayOfWeek == DayOfWeek.Sunday)
...
2018年4月19日 星期四
2018年4月10日 星期二
[Logic] Find the unique number
題目 :
There is an array with some numbers. All numbers are equal except for one. Try to find it!
findUniq([ 1, 1, 1, 2, 1, 1 ]) === 2
findUniq([ 0, 0, 0.55, 0, 0 ]) === 0.55
-----
my ans :
public static int GetUnique(IEnumerable<int> numbers)
{
var result = numbers.GroupBy(
c => c,
(key,g) => new { a = key, count = g.Count()})
.Where(c => c.count == 1)
.Select(c => c.a).Single();
return result;
}
There is an array with some numbers. All numbers are equal except for one. Try to find it!
findUniq([ 1, 1, 1, 2, 1, 1 ]) === 2
findUniq([ 0, 0, 0.55, 0, 0 ]) === 0.55
-----
my ans :
public static int GetUnique(IEnumerable<int> numbers)
{
var result = numbers.GroupBy(
c => c,
(key,g) => new { a = key, count = g.Count()})
.Where(c => c.count == 1)
.Select(c => c.a).Single();
return result;
}
2018年4月3日 星期二
[Logic] IQ Test
題目 :
Bob is preparing to pass IQ test. The most frequent task in this test is to find out which one of the given numbers differs from the others. Bob observed that one number usually differs from the others in evenness. Help Bob — to check his answers, he needs a program that among the given numbers finds one that is different in evenness, and return a position of this number.
! Keep in mind that your task is to help Bob solve a real IQ test, which means indexes of the elements start from 1 (not 0)
##Examples :
IQ.Test("2 4 7 8 10") => 3 // Third number is odd, while the rest of the numbers are even
IQ.Test("1 2 1 1") => 2 // Second number is even, while the rest of the numbers are odd
不加思索的弱code :
var list = numbers.Split(' ');
var temp = int.Parse(list[0]) % 2;
var temp2 = int.Parse(list[1]) % 2;
var count = 3;
for (int i = 2; i < list.Count(); i++)
{
var a = int.Parse(list[i])% 2;
if ( a != temp && temp == temp2 )
{
break;
}
else if ( a != temp && temp != temp2 )
{
count = 1;
break;
}
else if ( a != temp2 && temp != temp2 )
{
count = 2;
break;
}
count++;
}
return count++;
---
優雅的code :
var nums = numbers.Split(' ').Select(n => int.Parse(n));
var isEven = nums.Count(n => n % 2 == 0) == 1;
return nums.Select(n => n % 2 == (isEven ? 0 : 1)).ToList().IndexOf(true) + 1;
Bob is preparing to pass IQ test. The most frequent task in this test is to find out which one of the given numbers differs from the others. Bob observed that one number usually differs from the others in evenness. Help Bob — to check his answers, he needs a program that among the given numbers finds one that is different in evenness, and return a position of this number.
! Keep in mind that your task is to help Bob solve a real IQ test, which means indexes of the elements start from 1 (not 0)
##Examples :
IQ.Test("2 4 7 8 10") => 3 // Third number is odd, while the rest of the numbers are even
IQ.Test("1 2 1 1") => 2 // Second number is even, while the rest of the numbers are odd
不加思索的弱code :
var list = numbers.Split(' ');
var temp = int.Parse(list[0]) % 2;
var temp2 = int.Parse(list[1]) % 2;
var count = 3;
for (int i = 2; i < list.Count(); i++)
{
var a = int.Parse(list[i])% 2;
if ( a != temp && temp == temp2 )
{
break;
}
else if ( a != temp && temp != temp2 )
{
count = 1;
break;
}
else if ( a != temp2 && temp != temp2 )
{
count = 2;
break;
}
count++;
}
return count++;
---
優雅的code :
var nums = numbers.Split(' ').Select(n => int.Parse(n));
var isEven = nums.Count(n => n % 2 == 0) == 1;
return nums.Select(n => n % 2 == (isEven ? 0 : 1)).ToList().IndexOf(true) + 1;
2018年3月15日 星期四
2018年3月1日 星期四
[C#] winfrom 關閉後,後台程序依舊執行中問題
之前一直遇到這樣的問題,導致同仁無法正常更新
查了一下,似乎是舊程式沒有在form表單關閉時在觸發解構相關事件
所以補上了下述,看看之後情況會部會改善
private void frmMain_FormClosed(object sender, FormClosedEventArgs e)
{
//FormClosed事件程式碼
Dispose();
Application.Exit();
}
[C#] 西元年轉民國年格式
需求是將西元年日期轉為民國格式日期,以下做個method 紀錄
ex.2017/01/31 --> 106/01/31
public class DateHelper
{
/// <summary>
/// 解析成民國時間 ex.106/01/31
/// </summary>
/// <param name="datetime">西元時間</param>
/// <returns></returns>
public static string ParseToTaiwanDate(DateTime datetime)
{
System.Globalization.TaiwanCalendar tc = new System.Globalization.TaiwanCalendar();
return string.Format("{0}/{1}/{2}",
tc.GetYear(datetime),
datetime.Month.ToString("00"),
datetime.Day.ToString("00"));
}
public static string ParseToTaiwanYearMonth(DateTime datetime)
{
System.Globalization.TaiwanCalendar tc = new System.Globalization.TaiwanCalendar();
return string.Format("{0}/{1}",
tc.GetYear(datetime),
datetime.Month.ToString("00"));
}
public static string ParseToTaiwanYear(DateTime datetime)
{
System.Globalization.TaiwanCalendar tc = new System.Globalization.TaiwanCalendar();
return string.Format("{0}",
tc.GetYear(datetime));
}
}
[C#] 取得前一月的月初、月末日
ref : 並自行修改
DateTime FirstDay = DateTime.Now.AddMonths(-1).AddDays(-DateTime.Now.AddMonths(-1).Day+1);
DateTime LastDay = DateTime.Now.AddDays(-DateTime.Now.Day);
訂閱:
文章 (Atom)