Skip to content

Instantly share code, notes, and snippets.

View QMaximillian's full-sized avatar

Quinn Lashinsky QMaximillian

View GitHub Profile

a. First, I would fix the spelling of the function to is_palindrome and give the variables within the function more semantic meaning. (i.e. s could be string).

Next, I would tackle time and space complexity. In the first for loop we are recreating s which already exists and incurring an O(n^2) time complexity while doing it. This is because strings in Python are immutable. So our first loop is O(n) and the act of recreating our r string on each pass is also O(n) giving us an O(n^2) time complexity. Creating a new string in memory also gives us an O(n) space complexity.

Lastly, there is no reason to set our x value to True on each iteration. If the reversed string and the string do not match, then it isn't a palindrome and we can immediately return False.

I would improve this code by creating two pointers: a left index (0) and a right index (length of the string minus one). Then while the left index is less than the right index, I would check to see if the input string's value at

@QMaximillian
QMaximillian / gist:e7dcda51b74f059ea7dd72ceb84dd900
Last active March 25, 2021 17:09
ChangeMaker - HackerRank
function ChangeMaker(price, payment) {
    let priceInCents = Math.round(price * 100)
    let paymentInCents = payment.reduce((acc, curr) => { 
      return acc += curr
    }) * 100
    

  if (paymentInCents > priceInCents){
    console.log('can purchase, subtract the amount given from the price')
@QMaximillian
QMaximillian / markdown-compliant-link-styles.md
Last active March 4, 2021 17:11
Markdown Compliant Link Styles
const soundMachine = Machine({
id: 'sound',
initial: 'playRed',
states: {
playRed: {
entry: ['playRedSound']
},
}
@QMaximillian
QMaximillian / machine.js
Last active May 5, 2020 22:36
Generated by XState Viz: https://xstate.js.org/viz
const soundMachine = Machine({
id: 'soundMachine',
initial: 'choose',
states: {
choose: {
on: {
PLAY_RED: {
actions: (context, event) => console.log(context, events)
}
}
const countMachine = Machine({
id: 'doubleCounter',
initial: 'idle',
context: {
count: 0
},
states: {
idle: {
on: {
INC_COUNT_TWICE: {
@QMaximillian
QMaximillian / machine.js
Last active May 2, 2020 20:04
Generated by XState Viz: https://xstate.js.org/viz
// Available variables:
// - Machine
// - interpret
// - assign
// - send
// - sendParent
// - spawn
// - raise
// - actions
@QMaximillian
QMaximillian / SayHelloWithName Component
Last active February 14, 2019 21:11
Render Prop - SayHelloWithName
const SayHelloWithName = () => (
<Hello
render={({loud, handleLoudness }) => (
<h1>
<b onClick={handleLoudness}>
{loud ? `${word} Quinn` : `${word.toUpperCase()} Quinn `}
</b>
</h1>
)}
/>
@QMaximillian
QMaximillian / SayHello Component
Created February 14, 2019 03:49
Render Prop - SayHello
const SayHello = () => (
<Hello
render={({ loud, word, handleLoudness }) => (
<h1>
<b onClick={handleLoudness}>
{loud ? word : word.toUpperCase()}</b>
</h1>
)}
/>
);
@QMaximillian
QMaximillian / Hello Component
Last active February 14, 2019 21:23
Render Prop - Hello
const word = 'hello';
class Hello extends React.Component {
state={loud: false}
handleLoudness = () => {
this.setState({
loud: !this.state.loud
})