Programming Languages A(Coursera / University of Washington) Assignment 1

本文主要是介绍Programming Languages A(Coursera / University of Washington) Assignment 1,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Coursera有这门课的全部资料,就不放链接了:)
第一个课设一共13道题,都是sml编程题目 challenge没有时间就跳过了

原文件已上传到GitHub: 点这里
分数是90分

  1. Write a function is_older that takes two dates and evaluates to true or false. It evaluates to true if the first argument is a date that comes before the second argument. (If the two dates are the same, the result is false.)
    此处注意调用格式为: is_older([1999,08,28],[2000,08,28]);
fun is_older(xs : int list, ys : int list) =if null xsthen trueelseif (hd xs)>(hd ys)then falseelse is_older(tl xs,tl ys)
  1. Write a function number_in_month that takes a list of dates and a month (i.e., an int) and returns
    how many dates in the list are in the given month.
fun number_in_month(dates : (int*int*int) list , months : int) =if null datesthen 0elseif #2(hd dates)=monthsthen 1+number_in_month(tl dates,months)else number_in_month(tl dates,months)

这种格式是错误的:(#不可对变参列表使用)

fun number_in_months_(dates : int list ,  months : int) =#months(dates)
  1. Write a function number_in_months that takes a list of dates and a list of months (i.e., an int list)
    and returns the number of dates in the list of dates that are in any of the months in the list of months.
    Assume the list of months has no number repeated. Hint: Use your answer to the previous problem.
fun number_in_months(dates : (int*int*int) list , months : int list) =if null monthsthen 0	     else number_in_month(dates,hd months)+number_in_months(dates,tl months)
  1. Write a function dates_in_month that takes a list of dates and a month (i.e., an int) and returns a
    list holding the dates from the argument list of dates that are in the month. The returned list should
    contain dates in the order they were originally given.
fun dates_in_month(dates : (int*int*int) list , months : int) =if null datesthen []elseif #2(hd dates)=monthsthen (hd dates)::dates_in_month(tl dates,months)else dates_in_month(tl dates,months)
  1. Write a function dates_in_months that takes a list of dates and a list of months (i.e., an int list)
    and returns a list holding the dates from the argument list of dates that are in any of the months in
    the list of months. Assume the list of months has no number repeated. Hint: Use your answer to the
    previous problem and SML’s list-append operator (@).
fun dates_in_months(dates : (int*int*int) list , months : int list) =if null monthsthen []elsedates_in_month(tl dates,hd months)@dates_in_months(dates,tl months)
  1. Write a function get_nth that takes a list of strings and an int n and returns the nth element of the
    list where the head of the list is 1st. Do not worry about the case where the list has too few elements:your function may apply hd or tl to the empty list in this case, which is okay
fun get_nth(strings : string list , n : int) =if n = 1then (hd strings)elseget_nth(tl strings , n-1)
  1. Write a function date_to_string that takes a date and returns a string of the form January 20, 2013(for example). Use the operator ^ for concatenating strings and the library function Int.toString for converting an int to a string. For producing the month part, do not use a bunch of conditionals.
    Instead, use a list holding 12 strings and your answer to the previous problem. For consistency, put a comma following the day and use capitalized English month names: January, February, March, April,May, June, July, August, September, October, November, December.
fun date_to_string(year : int , month : int , day : int) =case month of1 =>  "January "  ^Int.toString(day)^", "^Int.toString(year)| 2 =>  "February " ^Int.toString(day)^", "^Int.toString(year)| 3 =>  "March "    ^Int.toString(day)^", "^Int.toString(year)| 4 =>  "April "    ^Int.toString(day)^", "^Int.toString(year)| 5 =>  "May "      ^Int.toString(day)^", "^Int.toString(year)| 6 =>  "June "     ^Int.toString(day)^", "^Int.toString(year)| 7 =>  "July "     ^Int.toString(day)^", "^Int.toString(year)| 8 =>  "August "   ^Int.toString(day)^", "^Int.toString(year)| 9 =>  "September "^Int.toString(day)^", "^Int.toString(year)| 10 => "October "  ^Int.toString(day)^", "^Int.toString(year)| 11 => "November " ^Int.toString(day)^", "^Int.toString(year)| 12 => "December " ^Int.toString(day)^", "^Int.toString(year);
  1. Write a function number_before_reaching_sum that takes an int called sum, which you can assume
    is positive, and an int list, which you can assume contains all positive numbers, and returns an int.
    You should return an int n such that the first n elements of the list add to less than sum, but the first
    n + 1 elements of the list add to sum or more. Assume the entire list sums to more than the passed in
    value; it is okay for an exception to occur if this is not the case.
fun number_before_reaching_sum(sum : int ,lists : int list) =if null lists orelse sum <= (hd lists) then 1else number_before_reaching_sum(sum-(hd lists),(tl lists))+1
  1. Write a function what_month that takes a day of year (i.e., an int between 1 and 365) and returns
    what month that day is in (1 for January, 2 for February, etc.). Use a list holding 12 integers and your
    answer to the previous problem.
fun what_month(day : int) =number_before_reaching_sum(day,[31,28,31,30,31,30,31,31,30,31,30,31]);
  1. Write a function month_range that takes two days of the year day1 and day2 and returns an int list
    [m1,m2,…,mn] where m1 is the month of day1, m2 is the month of day1+1, …, and mn is the month
    of day day2. Note the result will have length day2 - day1 + 1 or length 0 if day1>day2.
fun what_range(day1 : int , day2 : int) =if day1 > day2then []else what_month(day1)::what_range(day1+1,day2);
  1. Write a function oldest that takes a list of dates and evaluates to an (intintint) option. It
    evaluates to NONE if the list has no dates and SOME d if the date d is the oldest date in the list.
fun oldest(d : (int*int*int) list) = if null dthen NONEelselet val tmp_ans = oldest(tl d)inif isSome tmp_ans andalso (#1(valOf tmp_ans)) > (#1(hd d)) andalso (#2(valOf tmp_ans)) > (#2(hd d)) andalso (#3(valOf tmp_ans)) > (#3(hd d))then tmp_ans else SOME (hd d)end
  1. Challenge Problem: Write functions number_in_months_challenge and dates_in_months_challenge
    that are like your solutions to problems 3 and 5 except having a month in the second argument multiple
    times has no more effect than having it once. (Hint: Remove duplicates, then use previous work.)
(* quadratic algorithm rather than sorting which is nlog n *)
fun mem(x : int, xs : int list) =not (null xs) andalso (x = hd xs orelse mem(x, tl xs))
fun remove_duplicates(xs : int list) =if null xsthen []elselet val tl_ans = remove_duplicates (tl xs)inif mem(hd xs, tl_ans)then tl_anselse (hd xs)::tl_ansendfun number_in_months_challenge(dates : (int * int * int) list, months : int list) =number_in_months(dates, remove_duplicates months)fun dates_in_months_challenge (dates : (int * int * int) list, months : int list) =dates_in_months(dates, remove_duplicates months)fun reasonable_date (date : int * int * int) =let    fun get_nth (lst : int list, n : int) =if n=1then hd lstelse get_nth(tl lst, n-1)val year  = #1 dateval month = #2 dateval day   = #3 dateval leap  = year mod 400 = 0 orelse (year mod 4 = 0 andalso year mod 100 <> 0)val feb_len = if leap then 29 else 28val lengths = [31,feb_len,31,30,31,30,31,31,30,31,30,31]inyear > 0 andalso month >= 1 andalso month <= 12andalso day >= 1 andalso day <= get_nth(lengths,month)end
  1. Challenge Problem: Write a function reasonable_date that takes a date and determines if it
    describes a real date in the common era. A \real date" has a positive year (year 0 did not exist), a
    month between 1 and 12, and a day appropriate for the month. Solutions should properly handle leap
    years. Leap years are years that are either divisible by 400 or divisible by 4 but not divisible by 100.
    (Do not worry about days possibly lost in the conversion to the Gregorian calendar in the Late 1500s.)

这篇关于Programming Languages A(Coursera / University of Washington) Assignment 1的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/924017

相关文章

2014 Multi-University Training Contest 8小记

1002 计算几何 最大的速度才可能拥有无限的面积。 最大的速度的点 求凸包, 凸包上的点( 注意不是端点 ) 才拥有无限的面积 注意 :  凸包上如果有重点则不满足。 另外最大的速度为0也不行的。 int cmp(double x){if(fabs(x) < 1e-8) return 0 ;if(x > 0) return 1 ;return -1 ;}struct poin

2014 Multi-University Training Contest 7小记

1003   数学 , 先暴力再解方程。 在b进制下是个2 , 3 位数的 大概是10000进制以上 。这部分解方程 2-10000 直接暴力 typedef long long LL ;LL n ;int ok(int b){LL m = n ;int c ;while(m){c = m % b ;if(c == 3 || c == 4 || c == 5 ||

2014 Multi-University Training Contest 6小记

1003  贪心 对于111...10....000 这样的序列,  a 为1的个数,b为0的个数,易得当 x= a / (a + b) 时 f最小。 讲串分成若干段  1..10..0   ,  1..10..0 ,  要满足x非递减 。  对于 xi > xi+1  这样的合并 即可。 const int maxn = 100008 ;struct Node{int

2015多校联合训练第一场Assignment(hdu5289)三种解法

题目大意:给出一个数列,问其中存在多少连续子序列,子序列的最大值-最小值< k 这题有三种解法: 1:单调队列,时间复杂度O(n) 2:RMQ+二分,时间复杂度O(nlogn) 3:RMQ+贪心,时间复杂度O(nlogn) 一:RMQ+二分 RMQ维护最大值,最小值,枚举左端点i,二分找出最远的符合的右端点j,答案就是ans += j - i+1;(手推一下就知道) 比如1 2 3

Accept CS Ph.D. Offer from Stony Brook University,去SUNY石溪大学的CS Ph.D.啦

前言:在2017年3月24日,正式决定去纽约州立大学石溪分校(State University of New York, Stony Brook,简称石溪大学),CS Ph.D. 项目。本科直博,DIY申请,全额奖学金,第一年5.1万美元(免学费2.2万,2017 fall, 2018 spring 的TA 1.93万,2018 summer RA 1万,没有 Fellowship) Abs

2015 Multi-University Training Contest 5 1009 MZL#39;s Border

MZL's Border  Problem's Link:  http://acm.hdu.edu.cn/showproblem.php?pid=5351   Mean:  给出一个类似斐波那契数列的字符串序列,要你求给出的f[n]字符串中截取前m位的字符串s中s[1...i] = s[s.size()-i+1....s.size()]的最大长度。 analyse:   过计算

Nordic Collegiate Programming ContestNCPC 2021

Date:October 9, 2021 Dashboard - 2021-2022 ACM-ICPC Nordic Collegiate Programming Contest (NCPC 2021) - Codeforces Problem - C - Codeforces--Customs ControlsProblem - C - Codeforces- 题意:给定一个n个点,m条边

纪念一下第二个assignment 100分

感悟就是:坚持,才能从good到great。精益求精就是要不断打磨产品。 Princeton的课就是好,一个作业可以牵扯到很多算法。复习了shuffle算法和Resevoir Sampling算法,还有linkedin,array implement deque,iterator的用法,确实不错的课程,经典就是经典!刷题不在乎刷题数目多少,而在于刷背后知识点的深度和广度。加油!我觉得我刷完A

纪念一下自己的Coursera Princeton Algorithm的课程第一个assignment

今天终于完成了第一个Union-Find的assignment,之前觉得特别的难,可是最后自己也搞定了。而且是100%满分。 自己后来plot了一下自己的分数,也许这就是学习曲线吧。刚开始不会,到后来中期显著提高,但是要到100%,那就要经历更多的波折,甚至是下降都有可能。最后才能达到100%满分。 我觉得最有用的还是下面这段源代码: /*************************

2014 Multi-University Training Contest 1/HDU4861_Couple doubi(数论/规律)

解题报告 两人轮流取球,大的人赢,,, 贴官方题解,,,反正我看不懂,,,先留着理解 关于费马小定理 关于原根 找规律找到的,,,sad,,, 很容易找到循环节为p-1,每一个循环节中有一个非零的球,所以只要判断有多少完整循环节,在判断奇偶,,, #include <iostream>#include <cstdio>#include <cstring>