DSCI 510 UCLA ¿ Principles of Programming for Data Science Discussion
Description
Q1.[1 point]
Instruction : You are given a txt file named ‘weather_report.txt’ on D2L, download it and keep it in the same location where you have this notebook.
For this problem on files, You are required to count the number of lines in the given text file.
Input: none
Output: int
Rubric: All or Nothing.
Grading Rubric: 1 point if the output is correct. 0 otherwise (No Partial Marks).
<pre>
<span class="mtk8"># Fill this function for your solution</span><span class="mtk6">def</span> <span class="mtk14">number_of_lines</span><span class="mtk1">():</span><span class="mtk1"> fhand = <span class="mtk14">open</span><span class="mtk1">(</span><span class="mtk26">'weather_report.txt'</span><span class="mtk1">)</span><span class="mtk1"> count = <span class="mtk11">0</span><span class="mtk18">for</span><span class="mtk1"> line <span class="mtk6">in</span><span class="mtk1"> fhand:</span><span class="mtk1"> count = count + <span class="mtk11">1</span><span class="mtk14">print</span><span class="mtk1">(</span><span class="mtk26">'Line Count:'</span><span class="mtk1">, count)</span></span></span></span></span>
<pre>
<span class="mtk8"># Do not change this cell <span class="mtk14">print</span><span class="mtk1">(number_of_lines())</span></span>
Q2.[4 points]
For the file weather_report.txt, you have 4 floating point numbers in each line as per the format below.
(Min Temperature(in C), Max Temperature(in C), Humidity%, Rainfall(in)) for a particular day.
You are given a prediction algorithm which has the logic that if Max temperature – Min temperature is greater than 10 and if humidity is greater than 50%, it is a rainy day else it is sunny.
If we predict a day to be rainy using the above algorithm and actual rainfall for that day in the dataset is greater than 0 then it’s correct else it’s a wrong prediction. Similarly, if we predict sunny and rainfall is 0, then it’s correct else it’s a wrong prediction.
Your task is to return the total number of correct predictions corresponding to the dataset and the prediction algorithm.
Note: Read data from the file.
Input: none Output: int
Rubric : All/Nothing
Grading Rubric: 4 points if output is correct. (0, otherwise)
<pre>
<span class="mtk6">def</span> <span class="mtk14">model_accuracy</span><span class="mtk1">():</span><span class="mtk18">pass</span>
Q3 [2 points]
A String is determined to be fair if it satisfies any of the 3 conditions :
Every letter in the string is a capital, like “USC” (or) Every letter is not uppercase , like “assignment” (or) Only the initial character in the word is capital, like “This”.
Note : The input string will only have alphabets.
Input: String
Output: String
Example:
<code><span class="mtk1">Input <span class="mtk1">USC</span><span class="mtk1">Output <span class="mtk1">Fair</span></span></span></code>
<code><span class="mtk1">Input <span class="mtk1">USb <span class="mtk1">Output <span class="mtk1">Not Fair</span></span></span></span></code>
Grading Rubric: All or Nothing (+2 if All Cases pass, else 0)
<pre>
<span class="mtk6">def</span> <span class="mtk14">is_fair</span><span class="mtk1">(</span><span class="mtk15">s</span><span class="mtk1">):</span><span class="mtk18">pass</span>
Q4 [3 points]
Hopefully you all remember the work with Palindromes in the previous Lab (Lab-3). Given a list of words, your task is to return the first palindromic string in the list. If the list has no such matching string then return an empty string “”.
Note: You can reuse any of your own functions from the previous labs, if need be.
Definition : A string is said to be a palindrome if it reads the same in both forward and backward directions.
Input: List of Strings Output: String
Example:
<code><span class="mtk1">Input <span class="mtk1">words = ["asd","capital","aba","southern","califor</span><span class="mtk1">nia"]</span><span class="mtk1">Output <span class="mtk1">'aba'</span></span></span></code>
<code><span class="mtk1">Input <span class="mtk1">words = ["asddd","abcdefghi"] <span class="mtk1">Output <span class="mtk1">''</span></span></span></span></code>
Grading Rubric: All or Nothing (+3 if All Cases pass, else 0)
<pre>
<span class="mtk6">def</span> <span class="mtk14">firstPalindrome</span><span class="mtk1">(</span><span class="mtk15">words</span><span class="mtk1">):</span><span class="mtk18">pass</span>
<pre>
<span class="mtk8">#Sample example for you to test. Don't need to sub</span><span class="mtk8">mit this cell.</span><span class="mtk1">words = [</span><span class="mtk26">"asd"</span><span class="mtk1">,</span><span class="mtk26">"capital"</span><span class="mtk1">,</span><span class="mtk26">"aba"</span><span class="mtk1">,</span><span class="mtk26">"southern"</span><span class="mtk1">,</span><span class="mtk26">"california"</span><span class="mtk1">]</span>
<pre>
<span class="mtk8"># Again another sample for you to call your functi</span><span class="mtk8">on. Just for you to test</span><span class="mtk1">firstPalindrome(words)</span>
Bonus [2 points]
Our favourite detective Sherlock has a been given a cipher to decode and needs your help. The key to the cipher is to solve a substring problem. He’s given a String X, consisting of just 2 characters ‘a’ and ‘b’, and wants to find out the number of substrings of X which start and end with the character ‘a’.
Your task is to find out the number of such subtrings.
Input: String
Output: int
Example:
<code><span class="mtk1">Input <span class="mtk1">ababab</span><span class="mtk1">Output <span class="mtk1">6</span></span></span></code>
<code><span class="mtk1">Input <span class="mtk1">aa <span class="mtk1">Output <span class="mtk1">3</span></span></span></span></code>
Grading Rubric: All or Nothing (+2 if All Cases pass, else 0)
def count_substring(X):
pass
Have a similar assignment? "Place an order for your assignment and have exceptional work written by our team of experts, guaranteeing you A results."