Tumgik
rucode · 4 years
Photo
Tumblr media
Coding exercise from LeetCode:
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
Example:
Given nums = [2, 7, 11, 15], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9, return [0, 1].
If you can create a more a efficient way to do this, post it. I love learning new and different ways to approach a problem !
35 notes · View notes
rucode · 4 years
Photo
Tumblr media
How To Code: Messing Around with Pandas DataFrame in Python.
The dataset was downloaded from Kaggle, and contains information on characters from the lord of the rings.
https://www.kaggle.com/paultimothymooney/lord-of-the-rings-data
The CSV file was read into a dataframe, then we obtained the names of the column headers to see how the data was organized.
To see the unique values of the different types of races within the dataset, we can use the .unique() function i to find the unique elements of the column race.
I wanted to see how many characters of each race are represented in Lord of the Rings, so I used the .value_counts() function to get the amount, and then I used the .to_dict() function to create a dictionary with the race of the character as the key and the frequency count as the value.
I then wanted to see how many Elf characters are represented so I printed (raceCounts[’Elves’]) the value for elves.
Output:
Columns 
['birth' 'death' 'gender' 'hair' 'height' 'name' 'race' 'realm' 'spouse']
 Types of Races 
['Men' 'Orcs' 'Dragon' 'Elves' nan 'Hobbits' 'Dwarves' 'Dragons''Great Spiders' 'Black Uruk' 'Maiar' 'Ainur' 'Hobbit' 'Raven''Men,Wraith' 'Elf' 'God' 'Wolfhound' 'Half-elven' 'Men,Rohirrim''Maiar,Balrog' 'Werewolves' 'Dwarven' 'Goblin,Orc' 'Horse' 'Orc' 'Eagles''Uruk-hai' 'Great Eagles' 'Men,Skin-changer' 'Maiar,Balrogs''Uruk-hai,Orc' 'Orc,Goblin' 'Elves,Noldor' 'Drúedain' 'Urulóki''Ents,Onodrim' 'Skin-changer' 'Ents' 'Elves,Maiar' 'Balrog' 'Eagle''Dwarf' 'Stone-trolls' 'Vampire' 'Men,Undead' 'Half-elven,Men''Ainur,Maiar'] 
Elves:  103
90 notes · View notes
rucode · 5 years
Photo
Tumblr media
How To Code:  A basic way of reading files and printing their content, using C.
FILE *fopen(filename, "”r) : will open and read a file.
putchar(): will write a character to stdout.
Fortune Cookie:
The code is setup to read your “fortune”, by picking a random number and selecting a file with the number included in its name.
Output: 
Bad luck and misfortune will infest your pathetic soul for all eternity.
Output:
Good news is on the way, but not for you.
288 notes · View notes
rucode · 5 years
Photo
Tumblr media
How To Code: A basic application for linked lists, in java.
A linked list is a data structure, in which the elements are linked using pointers.
Output:
Name: Joanna Downing Blood Type: AB+ Z Virus antibody: Positve Exposure: Bloodborne Pathogen
Name: Armando Downing Blood Type: O+ Z Virus antibody: Positve Exposure: Trauma
Name: Regina Johnson Blood Type: A+ Z Virus antibody: Negative Exposure: N/A
Name: Steven Parker Blood Type: O+ Z Virus antibody: Positive Exposure: Unkown
301 notes · View notes
rucode · 5 years
Photo
Tumblr media
How To Code: Structs in C language.
Structs are data structures that allow the user to group items, which can differ in type. The following code is a basic example of the many uses for structs. It asks the user for measurement information pertaining to a flower’s physical attributes, and stores the data in the proper structures. 
Input:
Enter Length of sepal of the first sample, in cm. 4.9 Enter Length of petal of the first sample, in cm. 3.3 Enter Length of sepal of the second sample, in cm. 5.2 Enter Length of petal of the second sample, in cm. 3.5
Output:
Sample 1 Sepal Length: 4.90 Sample 1 Petal Length: 3.30 Sample 2 Sepal Length: 5.20 Sample 2 Petal Length: 3.50
259 notes · View notes
rucode · 5 years
Photo
Tumblr media
How to Code: Create a data frame in Python.
Data frames are 2-dimensional data structure, constructed in a tabular form with columns and rows.
Output:
  Fruit 0  Apple 1   Pear 2  Peach
  Col1  Col2  Col3 0     1     2     3 1     4     5     6 2     7     8     9
170 notes · View notes
rucode · 5 years
Photo
Tumblr media
How To Code:  My first Machine Learning model ! Using anaconda, and the  Scikit-learn libraries. Working with the ‘Iris’ dataset.
136 notes · View notes
rucode · 5 years
Photo
Tumblr media
How To Code: Frequency count for each letter in a word, in Java.
I used a HashMap in this code. 
Note: This code will count capital letters separate from lowercase letters, but one could easily convert all letters to lowercase to reduce confusion. 
Input/Output:
Enter a String apple {a=1, e=1, l=1, p=2}
..........................
Enter a String nincompoop {c=1, i=1, m=1, n=2, o=3, p=2}
68 notes · View notes
rucode · 5 years
Photo
Tumblr media
How To Code: Suff I just found out!
In Python, you can turn a string into a list, and use it as such.
Output:
A p p l e
307 notes · View notes
rucode · 5 years
Photo
Tumblr media
How To Code: A “Guess Number” game, using Python 3.
Input/Output:
You have 5 guesses Enter a number between 1 and 20: 10 Too High You have 4 guesses Enter a number between 1 and 20: 5 Too Low You have 3 guesses Enter a number between 1 and 20: 6 Too Low You have 2 guesses Enter a number between 1 and 20: 7 Too Low You have 1 guesses Enter a number between 1 and 20: 8 You Win
94 notes · View notes
rucode · 5 years
Photo
Tumblr media
How To Code: A “Guess Number” game, using C language.
Input/Ouput:
Enter a Number between 0 and 20: 10 Too Low You have 4 guesses. Enter a Number between 0 and 20: 15 Too Low You have 3 guesses. Enter a Number between 0 and 20: 18 Too High You have 2 guesses. Enter a Number between 0 and 20: 17 Too High You have 1 guesses. Enter a Number between 0 and 20: 16 You win
45 notes · View notes
rucode · 5 years
Photo
Tumblr media
How To Code: Basic “Guess Number” Game, using Java.
There are a lot more efficient ways to this, which can be found on the internet. I wanted to try do one without looking stuff up.
Input/Output:
Enter a Number between 1 and 20: You have 5 guesses. 15 Too High. Enter a Number between 1 and 20: You have 4 guesses. 10 Too High. Enter a Number between 1 and 20: You have 3 guesses. 5 Too High. Enter a Number between 1 and 20: You have 2 guesses. 3 You Win. Play Again.
Edit: I just realized that the .nextInt() used with the random import includes 0. So the print statement should state, “Enter a number between 0 and 20″.
190 notes · View notes
rucode · 5 years
Photo
Tumblr media
How to code: Finding vowels in a string, using Python 3.
Input/Output:
Enter a string: pineApplE There are 4 vowels
128 notes · View notes
rucode · 5 years
Photo
Tumblr media
How To Code: Finding vowels in a string, using C language.
The code prompts the user to enter a string (30 characters or less) and stores it into an array. The array is sent off to the “findVowels” function, where a ‘for loop’ is used to iterate through array to count the number of vowels.
Input/Ouput:
Enter a string pineApplE There are 4 vowels
48 notes · View notes
rucode · 5 years
Photo
Tumblr media
How To Code: Finding the amount of vowels in a string, using Java
The code above sends off a string to the function countVowels. Then a ‘for loop’ is used to run through the string, comparing each letter with “a e i o u” and “A E I O U”. If  a vowel is present the integer variable ‘counter’ is incremented. The counter is then returned and printed within main.
Output:
There are 4 Vowels
30 notes · View notes
rucode · 5 years
Photo
Tumblr media
How To Code: Find the largest value in a list, using Python 3.
Output:
Largest value in the list is 89
35 notes · View notes
rucode · 5 years
Photo
Tumblr media
How To Code: Find the Max number in an Array in C language.
The code above passes the array to the MaxFinder function. Then we create a ‘max’ variable and assign it to the value of the first element of the array. A ‘for loop’, then iterates through the array checking for larger values. If a larger value is found, the ‘max’ variable is then updated. There are many ways to create this type of function.
Output:
Max Value is 89
71 notes · View notes