Skip to content

Instantly share code, notes, and snippets.

@bharddwaj
bharddwaj / leetCode709.cpp
Last active June 9, 2019 22:20
Implement function ToLowerCase() that has a string parameter str, and returns the same string in lowercase.
class Solution {
public:
string toLowerCase(string str) {
for(int i = 0; i < str.size(); i++){
int ascii = (int)(str[i]);
if(ascii <= 90 and ascii > 64 ){ //means it's uppercase letter
str[i] = char(((int)(str[i])) + 32);
}
}
return str;
@bharddwaj
bharddwaj / leetcode5.cpp
Created June 9, 2019 00:28
Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.
class Solution {
public:
bool isPalindrome(string sub){
string other = "";
for(int i = (sub.size() - 1); i >= 0; i--){
other += sub[i];
}
return other == sub;
}
string longestPalindrome(string s) {
@bharddwaj
bharddwaj / leetCode344.cpp
Created June 2, 2019 19:23
Write a function that reverses a string. The input string is given as an array of characters char[]. Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory. You may assume all the characters consist of printable ascii characters.
class Solution {
public:
void reverseString(vector<char>& s) {
int length = s.size();
int j = length - 1;
int first_val;
for(int i = 0; i < length;i++){
if (i <= j){
@bharddwaj
bharddwaj / leetCode1.cpp
Last active June 2, 2019 18:43
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.
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
std::unordered_map<int,int> dict;
for(int i = 0; i < nums.size();i++){
int complement = target - nums[i];
if (dict.find(complement) != dict.end()){
//complement is in the unordered map
@bharddwaj
bharddwaj / fibonacci.cpp
Last active June 2, 2019 15:36
utilizing pass by reference in c++ for the first time to create memoized recursive fibonacci functions
//
// main.cpp
// Fibonacci
//
// Created by Bharddwaj on 5/31/19.
// Copyright © 2019 Bharddwaj. All rights reserved.
//
#include <iostream>
#include <map>
#include <chrono>
#include <iostream>
#define _USE_MATH_DEFINES
#include <math.h>
int main(int argc, const char * argv[]) {
// insert code here...
std::cout << "Hello, World!\n";
// const int sideLength = 2; Square Area 4
// const int radius = 1; Circle Area PI
import java.util.Random;
public class DNA {
// The genetic sequence
char[] genes;
char[] alpha = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',' '};
float fitness;
// Constructor (makes a random DNA)
public DNA(int num) {
Random r = new Random();
@bharddwaj
bharddwaj / naturalSelection.java
Last active January 9, 2019 13:37
natural selection function in case I need it
// Generate a mating pool
public void naturalSelection() {
// Clear the ArrayList
matingPool.clear();
Float [] fitnessArray = new Float[Population.size()];
float maxFitness = 0;
float fitness = 0;
float sum = 0;
for (int i = 0; i < Population.size(); i++) {
fitness = Population.get(i).fitness;
@bharddwaj
bharddwaj / halite.py
Last active October 27, 2018 14:55
this file contains code for my halite bot and I'll try to update it regularly and from bot 3 and on i add separate files
#!/usr/bin/env python3
# Python 3.6
# Import the Halite SDK, which will let you interact with the game.
import hlt
# This library contains constant values.
from hlt import constants
# This library contains direction metadata to better interface with the game.
@bharddwaj
bharddwaj / Main.java
Created October 13, 2018 21:01
attempt at the bloomberg api idk why i named it main lol but i'm too lazy to change it sometimes
package com.company; //idek how to java lol
import com.bloomberglp.blpapi.CorrelationID;
import com.bloomberglp.blpapi.Event;
import com.bloomberglp.blpapi.Message;
import com.bloomberglp.blpapi.MessageIterator;
import com.bloomberglp.blpapi.Request;
import com.bloomberglp.blpapi.Service;
import com.bloomberglp.blpapi.Session;