Can someone help me walk thru this function? Thank you

I am working on the guided project of “Mobile App for Lottery Addiction” from Stats.
According to the instruction, we should write a function named extract_numbers() that takes as input a row of the lottery dataframe and returns a set containing all the six winning numbers.
I know [4:10] refers to the column index of the dataframe we work on. But why is row[4,10]?
The argument of the function, as the input, is dataframe or the rows of the dataframe?
what does set(row.values) do here?

def extract_numbers(row):
    row = row[4:10]
    row = set(row.values)
    return row

As a beginner programmer and python is the first language I have ever learned. Sometimes I feel my programming skill still sucks. For the functions I wrote so far in DQ, I was able to write 90% of them under DQ’s detailed step by step instructions. But once without those detailed instructions or DQ just clarify a function’s objectives, I struggle to write one, especially if it involves statements such as if, while and a for loop.
I tried to practice my programming skills on codewars and start to look at some easy questions on leetcode. Those easy questions on leetcode are not easy for me. :frowning:
What is your suggestion? I may go over my python courses and some projects again.

1 Like

LeetCode easy question ranges from medium to hard.

If you struggle on doing LeetCode easy, then you have to work on your computer science fundamental knowledge and theory. This step cannot be skipped.There are no easy way out. You have to build the knowledge first.

You can follow the MIT lecture below.

It has to do with columns that we are interested in. That is, what columns are necessary to extract numbers or data that function needs. I agreed that row is bad used of label/variable name.

Depends on what is axis argument in the apply function.

If axis=1, the operation is a row operation.Then each input contains a single row entry with its columns.

If axis=0, the operation is a column operation.Then the input contains a column with all rows.

1 Like

set removes duplicate values. This gives unique values.

A set is a hash set which is a presentation of mathematical set.

set has a O(1) fast look up time for checking if an element is in a set. This is due to set is a hash set.

For Python, practice more. See below suggested learning path.

Leetcode, HackerRank and HackerEarth has challenging exercises.

CodingBat has easier problems. You might want to try this first.

https://codingbat.com/python

2 Likes

For pandas, there are videos on YouTube. See below for links.

You probably need a textbook reference. Check below for free online text. I suggest the 2nd and the last text given below.

First text, on basic example small program projects you can work on
https://runestone.academy/runestone/books/published/fopp/index.html

The last text on if statement
https://introcs.cs.princeton.edu/python/13flow/

The question is a word problem. Highlights all the keywords. To answer what the question wants, you have to rephrase into exactly straight to the point. Then each want in the problem represent each step in writing a code.

For not straightforward answer, you have to figure the process of doing what each step want.

I have updated all the above reply post. Please refresh. Thanks. Typing on a phone is difficult. Let me know if you need further help - comment below.

Best,
Alvin.

@alvinctk WOW, Thank you so much for the detailed information. I certainly will check that online MIT Algorithms course. I thought our DQ data science’s course design had a little bit touch on that topic.
Whenever I struggled writing a function, my husband suggested I draft a pseudo code first. But I have difficulty in even coming up with one. Maybe he is an engineer and I am not. Or maybe I just lack the background knowledge in Algorithms. Or maybe I need to familiarize with all the commonly used syntaxes.

I will certainly try codingbat. I am comfortable with some easy questions from codewars. I think I need to sharpen my programming skills before I get to Leetcode.

Again thank you so much! You are unbelievably so helpful!

1 Like