UTS Database


 Soal UTS

  • Buat Data Master Aplikasi Kasir dengan PHP & MongoDB yang collection terdiri dari:
  1. kategori_menu: _id, nama_kategori
  2. menu: _id, id_kategori, nama_menu, harga
  3. user: _id, username, password, level
  4. pelanggan: _id, nama, email, no_hp, alamat
  • Buat list menu link yang menghubungkan antar aplikasi crud & NAMA+NIM pada header aplikasi








5- Functions Fundamental


5- Functions Fundamental 


1- Functions


Instructions

Compute the sum of a_list (already defined in the code editor) without using sum().

Initialize a variable named sum_manual with a value of 0.
Loop through a_list, and for each iteration add the current number to sum_manual.
Print sum_manual and sum(a_list) to check whether the values are the same.


2- Built-in Function

Instructions

Generate a frequency table for the ratings list, which is already initialized in the code editor.

Start by creating an empty dictionary named content_ratings.
Loop through the ratings list. For each iteration:
If the rating is already in content_ratings, then increment the frequency of that rating by 1.
Else, initialize the rating with a value of 1 inside the content_ratings dictionary.
Print content_ratings.


3- Creating our own Functions

Instructions

Recreate the square() function above and compute the square for numbers 10 and 16.
Assign the square of 10 to a variable named squared_10.
Assign the square of 16 to a variable named squared_16.


4- The Structure of Function

Instructions

Create a function named add_10() that:

Takes a number as the input (name the input variable as you wish).
Adds the integer 10 to that number.
Returns the result of the addition.
Use the add_10() function to:

Add 10 to the number 30. Assign the result to a variable named add_30.
Add 10 to the number 90. Assign the result to a variable named add_90.



5- Parameters & Arguments

Instructions

Recreate the square() function by omitting the variable assignment step inside the function's body.

Without typing out the name of the parameter, use the new square() function to compute the square of the numbers 6 and 11.

Assign the square of 6 to a variable named squared_6.
Assign the square of 11 to a variable named squared_11.



6- Extract Values from any Column

Instructions

Write a function named extract() that can extract any column you want from the apps_data data set.

The function should take in the index number of a column as input (name the parameter as you want).
Inside the function's definition:

Create an empty list.
Loop through the apps_data data set (excluding the header). Extract only the value you want by using the parameter (which is expected to be an index number).
Append that value to the empty list.
Return the list containing the values of the column.
Use the extract() function to extract the values in the prime_genre column. Store them in a variable named genres. The index number of this column is 11.



7- Creating Frequency Tables

Instructions

Write a function named freq_table() that generates a frequency table for any list.

The function should take in a list as input.
Inside the function's body, write code that generates a frequency table for that list and stores the table in a dictionary.
Return the frequency table as a dictionary.
Use the freq_table() function on the genres list (already defined from the previous screen) to generate the frequency table for the prime_genre column. Store the frequency table to a variable named genres_ft.

Feel free to experiment with the extract() and freq_table() functions to easily create frequency tables for any column you want.



8- Writing a Single Function

Instructions

Write a function named freq_table() that generates a frequency table for any column in our iOS apps data set.

The function should take the index number of a column in as an input (name the parameter as you want).
Inside the function's body:
Loop through the apps_data data set (don't include the header row) and extract the value you want by using the parameter (which is expected to be an index number).
Build the frequency table as a dictionary.
The function should return the frequency table as a dictionary.
Use the freq_table() function to generate a frequency table for the user_rating column (the index number of this column is 7).
Store the table in a variable named ratings_ft.



9- Reusability & Multiple Parameters

Instructions

Update the current freq_table() function to make it more reusable.

The function should take in two inputs this time: a data set and the index of a column (name the parameters as you want).
Inside the function's body:
Loop through the data set using that parameter which is expected to be a data set (a list of lists). For each iteration, select the value you want by using the parameter which is expected to be an index number.
Build the frequency table as a dictionary.
The function should return the frequency table as a dictionary.
Use the updated freq_table() function to generate a frequency table for the user_rating column (the index number of this column is 7). Store the table in a variable named ratings_ft.



10- Keyword & Positional Arguments

Instructions

Use the freq_table() function to generate frequency tables for the cont_rating, user_rating, and prime_genre columns.
Use positional arguments when you generate the table for the cont_rating column (index number 10). Assign the table to a variable named content_ratings_ft.
Use keyword arguments for the user_rating column (index number 7) following the order (data_set, index). Assign the table to a variable named ratings_ft.
Use keyword arguments for the prime_genre column (index number 11) following the order (index, data_set). Assign the table to a variable named genres_ft.



11- Combining Functions

Instructions

Write a function named mean() that computes the mean for any column we want from a data set.

The function should take in two inputs: a data set and an index value.
Inside the body of the mean() function, use the extract() function to extract the values of a column into a separate list, and then compute the mean of the values in that list using find_sum() and find_length().
The function should return the mean of the column.
Use the mean() function to compute the mean of the price column (index number 4). Assign the result to a variable named avg_price.



12- Debugging Functions

Instructions

The code we provided in the code editor has several bugs (errors). Run the code, and then use the information provided in the tracebacks to debug the code.

Select all lines of code and press ctrl + / (PC) or ⌘ + / (Mac) to uncomment it so you can modify it.
For a demo of how this keyboard shortcut works, see this help article.
To understand what the bug is, read the general description of the error.
To understand where the bug is, follow the red arrows.
You'll see the arrows are represented as ---> or ^.
Note that there's more than one bug in the code we wrote. Once you debug an error, you'll get another error. This doesn't mean you're not making progress, on the contrary — you're closer to debugging the code completely.





MongoDB for VSCode

 

MongoDB for VSCode



MongoDB for VSCode adalah ekstensi yang memungkinkan pengembang untuk terhubung ke database MongoDB, menjelajahi data, mengeksekusi query, dan mengelola koleksi data, semuanya tanpa perlu meninggalkan VSCode. 

Instalasi MongoDB for VSCode

Langkah pertama adalah menginstal ekstensi MongoDB for VSCode. Ikuti langkah-langkah ini:

1. Buka Visual Studio Code.

2. klik ekstensi 

3. Cari "MongoDB for VSCode" kemudian  instal ekstensi tersebut.

Setelah berhasil menginstal ekstensinya, MongoDB for VSCode siap untuk digunakan.


Menghubungkan ke Database MongoDB

Setelah Anda menginstal ekstensi MongoDB for VSCode, langkah selanjutnya adalah menghubungkan VSCode ke database MongoDB. Anda dapat melakukannya dengan mengikuti langkah-langkah berikut:

1. Buka tab "MongoDB" yang ada di panel bagian kiri.

2. Klik tombol "Add Connection/Tambah koneksi" dan masukkan koneksi URL MongoDB, nama koneksi, dan autentikasi jika diperlukan.

3. Klik "Connect/hubungkan" untuk menghubungkan VSCode ke database MongoDB.


Kuis

1. Kerjakan modul 3 (operator pembanding) dan modul 4 (operator logika) dengan menggunakan mongodb for vscode.
2. Screnshoot query dan hasilnya lalu upload di blog masing-masing 

Operator Pembanding

1. Operator $eq








2. Operatpr $gt








3. Operator $gte









Operator Logika

1. Operator  $and









2. Operator $or








3. Operator $not









4. Operator $exist


Dictionaries and Frequency Tables

 

Dictionaries and Frequency Tables


1.Storing Data
Store the data in the table above using two different lists.
Assign the list ['4+', '9+', '12+', '17+'] to a variable named content_ratings.
Assign the list [4433, 987, 1155, 622] to a variable named numbers.
Store the data in the table above using a list of lists. Assign the list [['4+', '9+', '12+', '17+'], [4433, 987, 1155, 622]] to a variable named content_rating_numbers.

content_ratings = ['4+', '9+', '12+', '17bgnbgnnb












2.Dictionaries
Map content ratings to their corresponding numbers by recreating the dictionary above: {'4+': 4433, '9+': 987, '12+': 1155, '17+': 622}. Assign the dictionary to a variable named content_ratings.
Print content_ratings and examine the output carefully. Has the order we used to create the dictionary been preserved? In other words, is the output identical to {'4+': 4433, '9+': 987, '12+': 1155, '17+': 622}? We'll discuss more about this on the next screen.

content_ratings = {'4+': 4433, '9+': 987, '12+': 1155, '17+': 622}






3.Indexing
Retrieve values from the content_ratings dictionary.

Assign the value at index '9+' to a variable named over_9.
Assign the value at index '17+' to a variable named over_17.
Print over_9 and over_17.

content_ratings = {'4+': 4433, '9+': 987, '12+': 1155, '17+': 622}









4.Alternative Way of Creating a Dictionary
Use the new technique we learned to map content ratings to their corresponding numbers inside a dictionary.

Create an empty dictionary named content_ratings.
Add the index:value pairs one by one using the dictionary_name[index] = value technique. This should be the final form of the dictionary: {'4+': 4433, '9+': 987, '12+': 1155, '17+': 622}.
Retrieve the value at index 12+ from the content_ratings dictionary. Assign it to a variable named over_12_n_apps.

content_ratings = {}
content_ratings['4+']=4433















5.Key Value Pair
Create the following dictionary and assign it to a variable named d_1:
{'key_1': 'first_value',
'key_2': 2,
'key_3': 3.14,
'key_4': True,
'key_5': [4,2,1],
'key_6': {'inner_key' : 6}
}
2.Examine the code below and determine whether it'll raise an error or not. If you think it'll raise an error, then assign the boolean True to a variable named error, otherwise assign False.

{4: 'four',
1.5: 'one point five',
'string_key': 'string_value',
True: 'True',
[1,2,3]: 'a list',
{10: 'ten'}: 'a dictionary'}




6.Checking for Memberships
Using the in operator, check whether the following values exist as dictionary keys in the content_ratings dictionary:

The string '9+'. Assign the output of the expression to a variable named is_in_dictionary_1.
The integer 987. Assign the output of the expression to a variable named is_in_dictionary_2.
Combine the output of an expression containing in with an if statement. If the string '17+' exists as dictionary key in content_ratings, then:

Assign the string "It exists" to a variable named result.
Print the result variable.

content_ratings = {'4+': 4433, '9+': 987, '12+': 1155, '17+': 622}
is_in_dictionary_1 = '9+' in content_ratings















7.Counting with Dictionaries
Count the number of times each unique content rating occurs in the data set.
Create a dictionary named content_ratings where the keys are the unique content ratings and the values are all 0 (the values of 0 are temporary at this point, and they'll be updated).
Loop through the apps_data list of lists. Make sure you don't include the header row. For each iteration of the loop:
Assign the content rating value to a variable named c_rating. The content rating is at index number 10 in each row.
Check whether c_rating exists as a key in content_ratings. If it exists, then increment the dictionary value at that key by 1 (the key is equivalent to the value stored in c_rating).
Outside the loop, print content_ratings to check whether the counting worked as expected.

opened_file = open('AppleStore.csv', encoding="utf-8")
from csv import reader
read_file = reader(opened_file)
apps_data = list(read_file)

















8.Finding the Unique Values
Count the number of times each unique content rating occurs in the data set while finding the unique values automatically. Create an empty dictionary named content_ratings.
Loop through the apps_data list of lists (make sure you don't include the header row). For each iteration of the loop:
Assign the content rating value to a variable named c_rating. The content rating is at index number 10.
Check whether c_rating exists as a key in content_ratings.
If it exists, then increment the dictionary value at that key by 1 (the key is equivalent to the value stored in c_rating).
Else, create a new key-value pair in the dictionary, where the dictionary key is c_rating and the dictionary value is 1.
Outside the loop, print content_ratings to check whether the counting worked as expected.


opened_file = open('AppleStore.csv', encoding="utf-8")
from csv import reader
read_file = reader(opened_file)
apps_data = list(read_file)
content_ratings = {}














9.Proportions and Percentage
Count the number of times each unique genre occurs.
Create an empty dictionary named genre_counting.
Loop through the apps_data list of lists (make sure you don't include the header row). For each iteration of the loop:
Assign the genre to a variable named genre. The genre comes as a string and has the index number 11.
Check whether genre exists as a key in genre_counting.
If it exists, then increment the dictionary value at that key by 1 (the key is equivalent to the value stored in genre).
Else, create a new key-value pair in the dictionary, where the dictionary key is genre and the dictionary value is 1.
Outside the loop, print genre_counting and try to determine what's the most common app genre in our data set.

opened_file = open('AppleStore.csv', encoding="utf-8")
from csv import reader
read_file = reader(opened_file)
apps_data = list(read_file)

















10.Looping Over Dictionaries
Loop over the content_ratings dictionary and transform the frequencies to percentages. For every iteration of the loop:

Transform the dictionary value (the frequency) to a proportion by dividing it by the total number of apps.
Transform the updated dictionary value (the proportion) to a percentage by multiplying it by 100.
Find out the percentage of apps that have a content rating of '17+'. Assign your answer to a variable named percentage_17_plus.

Find out the percentage of apps that can be downloaded by a 15-year-old. Assign your answer to a variable named percentage_15_allowed.















11.Keeping the Dictionaries Separate
Transform the frequencies inside content_ratings to proportions and percentages while creating separate dictionaries for each.

Assign the dictionary storing proportions to a variable named c_ratings_proportions.
Assign the dictionary storing percentages to a variable named c_ratings_percentages.
Optional challenge: try to solve this exercise using a single for loop (solution to this challenge provided).











12.Frequency Tables for Numerical Columns
Extract the values in the size_bytes column in a separate list.

Create an empty list named data_sizes.
Loop through apps_data (make sure you don't include the header row) and for every iteration:
Store the data size as a float in a variable named size (the index number for the data size is 2).
Append size to the data_sizes list.
Find out the minimum and the maximum app data size.

Assign the minimum value to a variable named min_size.
Assign the maximum value to a variable named max_size.

13.Filtering for the Intervals
Begin by finding the minimum and maximum value in the rating_count_tot column.

Extract the values in the rating_count_tot column (index number 5) in a separate list (don't forget to convert to integer or float).
Find out the minimum and maximum value of that list using the min() and the max() commands.
Based on the minimum and maximum value you've found, choose a few intervals (try to choose five intervals or less).

We've disabled answer checking for this exercise to give you the freedom to choose the intervals you find suitable (there's not a fixed solution for this exercise). You can see the intervals we chose in the solution.
Once you've chosen the intervals, compute the frequency of apps for each interval. Store the frequency table in a dictionary.

Create a dictionary with intervals as dictionary keys and zeros as dictionary values.
Loop through the apps_data data set. Count the frequency of each interval using an if statement followed by a series of elif clauses.
Inspect the frequency table and analyze the results.

opened_file = open('AppleStore.csv', encoding="utf-8")
from csv import reader
read_file = reader(opened_file)
apps_data = list(read_file)

rating_count = []
for row in apps_data[1:]:
    rating_count_tot = float(row[5])
    rating_count.append(rating_count_tot)
ratings_min = min(rating_count)
ratings_max = max(rating_count)