Skip to content

Instantly share code, notes, and snippets.

View willy-r's full-sized avatar
👋
Hello, there!

William Rodrigues willy-r

👋
Hello, there!
  • Pacujá, Ceará, Brazil
View GitHub Profile
@willy-r
willy-r / missing_ints.py
Last active September 22, 2020 01:55
Given a list of ordered integers with some of the numbers missing (and with possible duplicates), find the missing numbers.
def missing_ints(numbers):
"""Given a list of ordered integers with some of the
numbers missing (and with possible duplicates), find
the missing numbers.
"""
if not numbers:
return []
missing_numbers = [n for n in range(min(numbers), max(numbers))
if n not in numbers]
return missing_numbers
@willy-r
willy-r / determine_primes.py
Last active September 19, 2020 16:53
Determines prime numbers in a list of integers.
from math import sqrt
def determine_primes(given_numbers):
"""Determines prime numbers in a list of integers."""
primes = []
for possible_prime in given_numbers:
is_prime = True
for num in range(2, int(sqrt(possible_prime)) + 1):
if possible_prime % num == 0:
@willy-r
willy-r / logic_gates.py
Last active September 19, 2020 16:48
Write a program that implements logic gates AND, OR, NOT, NAND, NOR, XOR, and XNOR.
def and_gate(x, y):
"""The AND gate gives an output of 1 if both
the two inputs are 1, it gives 0 otherwise.
"""
if x == 1 and y == 1:
return 1
return 0
def or_gate(x, y):
@willy-r
willy-r / twitter_sample.py
Last active May 25, 2020 17:50
Design a simplified version of Twitter, where users can post tweets, follow/unfollow others, and is able to see the 10 most recent tweets in the user’s news feed.
class User:
def __init__(self, user_name):
self.user_name = user_name
self.tweets = []
self.follows = []
def __str__(self):
return self.user_name
@willy-r
willy-r / simple_text_editor.py
Last active May 29, 2020 21:24
Build a simple text editor
"""This is a simple script that simulates a text editor."""
import re
import os.path
from datetime import datetime
class TextEditor:
"""A tentative of representing a simple text editor.
@willy-r
willy-r / num_chars.py
Created August 25, 2020 18:49
Given a string s and a character c, return the number of occurrences of c in s.
def num_chars(s: str, c: str):
"""Returns the number of occurrences of c in s."""
return s.lower().count(c.lower())
@willy-r
willy-r / stock_buy_sell.py
Last active September 4, 2020 18:18
Given an array of numbers that represent stock prices (where each number is the price for a certain day), find 2 days when you should buy and sell your stock for the highest profit.
def stock_buy_sell(prices: list):
"""Returns the 2 days when you should buy and sell the stock for the
highest profit.
"""
buy_day = prices.index(min(prices)) + 1
sell_day = prices.index(max(prices[buy_day:])) + 1
return f'Buy on day {buy_day}, sell on day {sell_day}'
if __name__ == '__main__':
@willy-r
willy-r / pig_latin.py
Last active September 10, 2020 10:21
Write a function that converts a sentence into Pig Latin (https://en.m.wikipedia.org/wiki/Pig_Latin#Rules).
def _to_pig_latin(word: str):
"""Converts the word into Pig Latin."""
VOWELS = 'aeiou'
if word[0].lower() in VOWELS:
return f'{word}yay'
consonants = 0
for letter in word.lower():
if letter not in VOWELS:
consonants += 1
@willy-r
willy-r / fibonacci_like.py
Created September 17, 2020 18:17
Given an array of increasing integers, find the length of the longest fibonacci-like subsequence of the array. If one does not exist, return 0. A sequence is “fibonacci-like” if X_i + X_{i+1} = X_{i+2}.
def fibonacci_like(arr: list):
"""Returns the length of the longest fibonacci-like subsequence of the
arr.
If one does not exist, returns 0.
"""
seen_arr = set(arr)
max_length = 0
for i in range(len(arr)):
for j in range(i + 1, len(arr)):
@willy-r
willy-r / sampleREADME.md
Created September 17, 2020 23:12 — forked from FrancesCoronel/sampleREADME.md
A sample README for all your GitHub projects.

FVCproductions

INSERT GRAPHIC HERE (include hyperlink in image)

Repository Title Goes Here

Subtitle or Short Description Goes Here