#algos-and-data-structs

1 messages · Page 99 of 1

lean dome
#

If you've got an iterable over tuples, and you want to pass each tuple to a function that accepts a tuple, just don't unpack the tuples at all.

rancid patio
#

this 👍

dense cloud
#

Hello guys! I have a question regarding DFS/BFS.
I understand that DFS is backtracking/ exploring, and BFS is finding a pathway. But how do i determine when to use either one?

vast halo
#

is anyone available to help with a for loop problem please ping me

fiery cosmos
#

does anybody

#

know

#

how to implement karatsuba algorithm in python

#

switching

#

from cpp to python

onyx root
fiery cosmos
#

idk

#

learning

#

for cpp

#

*cp

onyx root
#

CPython uses karatsuba for very large numbers

kindred coyote
#

is there anyway to make this code faster: ```py
import sys
raw_input = sys.stdin.readline
m = int(raw_input())
n = int(raw_input())
brushes = raw_input()
stroke = []

colors = [['B' for _ in range(n)] for _ in range(m)]
for i in range(int(brushes)):
g = raw_input().split(' ')
if stroke.count(g) == 1:
stroke.remove(g)
else:
stroke.append(g)
def changeColumn(num,colors,index):
if num == 0:
return colors
colors[num-1][index] = 'G' if colors[num-1][index] == 'B' else 'B'
num -= 1
changeColumn(num,colors,index)

def countGold(c,options,i):
if options == []:
s = 0
for l in c:
s += l.count("G")
print(s)
return
area = options[i][0]
times = int(options[i][1]) - 1
if area == "R":
c[times] = list(''.join(c[times]).replace("G","A").replace("B","G").replace("A","B"))
elif area == "C":
changeColumn(m,c,times)

options.remove(options[i])
countGold(c,options,i)

countGold(colors,stroke,0)

onyx root
#

@kindred coyote changeColumn is recursive, which is unusual. you can use a loop.

#

also, it returns a value, but nothing seems to use it?

kindred coyote
# onyx root is it too slow?

Actually I found the problem. It's generating colors. A program will need to generate a 10^6 x 10^6 array in less than 2-3 seconds

#

I need to solve this problem without generating an array

#

as that takes up too much time

dapper sapphire
#

is there a good resource to learn syntax of python linked list? I understand the concept theoretically that the current node points to memory address of the next node and so on. But I find it a bit confusing to implement it. And every other website I go to has a different way of implementing it.

brisk saddle
#

@dapper sapphire it is pretty simple.

in essence, you have a head node, and a tail node.

#

let us say you have these 5 values that you wish to initiate with the Linked List:
1, 2, 3, 4, 5

#

and you have a head field, and a tail field in your class.

#

the tail represents the last node in the Linked List, while the head represents the first.

#

do you follow so far?

dreamy arrow
#

he wanted an answer for exams and its wright because for each time in n you will move length -1 which means you will move length *n

dapper sapphire
brisk saddle
#

nope, the tail actually points to the first element once it is initialized.

#

so normally when i implement my linked lists, i pop(0) the first element off the list of initialized values, and assign that to the head, and the tail to that as well.

#

once you do that, you begin to "chain" values.

#
def __init__(self, *entries):
    entries = list(entries)
    self.head = Node(entries.pop(0))
    self.tail = self.head
#

so when you "chain" values, you loop through the remaining values to initialize, and for each element, you do the following:

create a new node. we will call it n for now.
set the next_node field of the current tail to n
set the tail to n

#

this is so the cycle can repeat for the next node you add.

#

@dapper sapphire let me know if you want me to rephrase anything, or you do not understand it.

dapper sapphire
#

wait can we do *enteries? I thought we cant use a pointer in Python. and would having a pointer or not having a pointer make a difference in this case?

brisk saddle
#

*entries defines a varadic argument called entries

#

the * does not mean "pointer to type entries" like it does in C.

#

it just means "this is a varadic argument."

#

this is so you could do this, and have it be valid syntax:

my_list = LinkedList(1, 2, 3, 4, 5)

instead of

my_list = LinkedList([1, 2, 3, 4, 5])
#

you following with that?

dapper sapphire
#

oh right ok I get it so with variadic argument we dont have to worry about specifying the number of parameters.
on line 3 you pop the 0th element and assign the value to head. On line 4 you assign the popped value also to the tail.

brisk saddle
#

yup!

#

let me know if you need help with any other datastructures, or you want to clear some things up.

#

i would be glad to help.

dapper sapphire
#

wait is that it?? That's all for linked list?

brisk saddle
#

yeah pretty much.

#

that is all it takes to initialize one.

#

there are other operations, and you also need to setup iteration, i can help you with that if you would like.

#

but yeah, that is basically all it takes to create one. Linked Lists are extremely easy to initialize.

dapper sapphire
#

So what you just did with 4 lines of code to initialize a linked list, is this similar to the below code where we create a Node class and a LinkedList class:

class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

class LinkedList:
    def __init__(self):
        self.head = None
brisk saddle
#

yeah that is pretty similar to how i would write it.

#

i mean i could show you how to setup a Linked List in C as well if you wanted me to, but this is a Python server.. the option is always there, though!

#

assuming you do not know.

dapper sapphire
#

I think C would just confuse me even though I have programmed in it before. Python would be fine. But hey when you do:

self.head = Node(entries.pop(0))
self.tail = self.head

You are not pointing anything to None? both head and tail point to the 0th element.

brisk saddle
#

yeah, that is intentional.

#

it is because if we start looping through with the tail as None, we would have no way to add something to the end of the list without using the head field.

#

we do not want to reassign the head field during initialization.

dapper sapphire
#

ok so how would you create an add method?

#

to insert elements to the list.

brisk saddle
#

what form of adding? insertion, or appending?

dapper sapphire
#

appending

brisk saddle
#

oh, appending is easy. you do the exact same thing you did during initialization with the tail field.

#

except it is in it's own function.

dapper sapphire
#

are you creating function based linked list?

brisk saddle
#

well of course. classes have methods bound to them.

dapper sapphire
#

I mean I guess, we could create a linked list either way.

brisk saddle
#

or by "function-based" did you mean in a way similar to how C operates on datastructures?

dapper sapphire
#

so suppose the code you wrote was in a class called Node, then would we create a add method like below:

def add(self, data):
  self.head = Node(data)
brisk saddle
#

using functions in a file that accepts a struct or something along those lines as an argument?

#

that seems more like a prepend method, but yeah, close.

#

you are missing an important factor, though.

#

that will essentially unlink the entire Linked List, since you are not assigning the next field of the new node to the previous head.

#

so make sure you do that.

dapper sapphire
#
def add(self, data):
  temp = Node(data)
  temp.next = self.head
  self.head = temp     

So something like this.

brisk saddle
#

bingo

#

whenever you add or remove any Nodes into your Linked List, make sure you check you are not preventing access to other parts of the list.

#

just a general word of advice, really.

dapper sapphire
#

How did you get comfortable with linked lists? Just practice?

brisk saddle
#

more or less, yeah

#

i am familiar with a few more datastructures.

#

Hashtables, Queues, Stacks, most types of Trees, Sets, and Arrays.

#

those are the ones i am aware of how to create.

#

Queues and Stacks are essentially just Linked Lists or Arrays with operations that only operate on the bottom or top.

dapper sapphire
#

Yeah I was doing Queues and Stacks yesterday and I found those pretty easy to understand and implement.

brisk saddle
#

they are, yeah.

#

Hashtables and Trees are my favorite to implement.

dapper sapphire
#

When you say you implemented a hashtable, you mean, you created a dictionary?

brisk saddle
#

yeah, Hashtables are an implementation of a Dictionary, also known as Associative Arrays.

dapper sapphire
#

Hashtables are next on my list of things to implement. Because I use dictionaries a lot on different projects and I find it really powerful. Hey man, really appreciate all the help!!

brisk saddle
#

anytime. feel free to ask me for help on implementing them Hashtables. i would be glad to help out.

dapper sapphire
#

I am glad that I scrolled down to the server and posted the question here. Yeah of course, much appreciated!

brisk saddle
#

anyway, i think i am going to head off to bed now.

#

sleep tight.

icy crane
#

Hm

#

.topic does it work here

grand havenBOT
#
**No topics found for this channel.**

Suggest more topics here!

icy crane
#

Lolll

dreamy arrow
#

get it at the base case condition of the recursive function

knotty void
#

are array lists better than single linked list when you want to store a very big data base?

kindred coyote
#
import sys
raw_input = sys.stdin.readline
m = int(raw_input())
n = int(raw_input())
brushes = int(raw_input())

listed_rows = []
listed_columns = []
gold = 0
duplicates = []
for i in range(brushes):
    g = raw_input().split()
    g[1] = int(g[1])
    if g in duplicates:
        continue
    else:
        duplicates.append(g)
        if g[0] == "R":
            gold += (n - len(listed_columns))
            gold -= len(listed_columns)
            listed_rows.append(g[1])
        if g[0] == "C":
            gold += (m - len(listed_rows))
            gold -= len(listed_rows)
            listed_columns.append(g[1]-1)
print(gold)
``` does any1 know how to make this even faster? I still get a time limit exceeded error
#

(Time Limit is 4 seconds)

kindred coyote
#

lemme send problem

#
A new and upcoming artist has a unique way to create checkered patterns. The idea is to
use an M-by-N canvas which is initially entirely black. Then the artist repeatedly chooses
a row or column and runs their magic brush along the row or column. The brush changes
the colour of each cell in the row or column from black to gold or gold to black.
Given the artist’s choices, your job is to determine how much gold appears in the pattern
determined by these choices.
#
The first line of input will be a positive integer M. The second line of input will be a positive
integer N. The third line of input will be a positive integer K. The remaining input will be
K lines giving the choices made by the artist. Each of these lines will either be R followed
by a single space and then an integer which is a row number, or C followed by a single space
and then an integer which is a column number. Rows are numbered top down from 1 to M.
Columns are numbered left to right from 1 to N.
#

and u output how many gold squares there r

#

I also have this code: ```py
for i in range(int(brushes)):
g = raw_input().split(' ')
if stroke.count(g) == 1:
stroke.remove(g)
else:
stroke.append(g)

def changeColumn(num,colors,index):
if num == 0:
return colors
colors[num-1][index] = 'G' if colors[num-1][index] == 'B' else 'B'
num -= 1
changeColumn(num,colors,index)

def countGold(c,options,i):
if options == []:
f = 0
for l in c:
f += l.count("G")
print(f)
return
area = options[i][0]
times = int(options[i][1]) - 1
if area == "R":
c[times] = list(''.join(c[times]).replace("G","A").replace("B","G").replace("A","B"))
else:
changeColumn(m,c,times)

options.remove(options[i])
countGold(c,options,i)

countGold(colors,stroke,0)

knotty void
#

are you sure you need to iterate through the entire list?

kindred coyote
#

It's basically saying this: ```
Enter a number M
The next M lines, enter....

#

so in the 1st code, the for loop is a must unless there's another way to do that

winter schooner
#

can anyone help me converting this bash to python

#

#!/bin/bash

packageInfo.bash

Purpose: Generates a report to displaying specified information of installed software

USAGE: ./packageInfo.bash [application-name]

Author: *** INSERT YOUR NAME ***

Date: *** CURRENT DATE ***

if [ $(whoami) != "root" ] # only runs if root
then
echo "You must be logged in as root." >&2
exit 1
fi

if [ $# -ne 1 ]
then
echo "Your command must have a application-name as argument" >&2
echo "USAGE: $0 [application-name]" >&2
exit 1
fi

Create report title (echo with -e option allows newline \n character to be used)

echo -e "\nSOFTWARE PACKAGE INFORMATION REPORT" > /root/package-info.txt
echo -e "Date: $(date +'%A %B %d, %Y (%H:%M:%p)')\n\n " >> /root/package-info.txt

Clear screen and use Here Document to display select on report items to read into variable

clear
cat <<+
Available Package Information Items:

Name
Summary
Version
License
Source
URL
+
read -p "Enter word(s) shown above separated by spaces: " choice

Convert spaces to pipe symbol (|)

processedChoice=$(echo $choice | sed 's/ /|/g')

Use sed with extended regular expressions to only print those matching report elements

rpm -qi $1 | sed -r -n "/^($processedChoice)/ p" >> /root/package-info.txt

cat <<+
File "/root/package-info.txt" has been created
+

Save, set permissions, and then run that shell script for the application gedit. Did it create that report? Try running the script without an argument - What did it do?
Use the wget command to download, study, and run the following shell scripts on-line:

worn narwhal
#

What is an algorithm? My understanding of writing algorithms is figuring out what exactly to do with code to find the results you want. Is this an accurate definition?

topaz pulsar
#

@kindred coyote can you send a link to this problem, I want to test a solution I think i have using ||sets||

dry crow
#

same

kindred coyote
# topaz pulsar <@!629120968425472010> can you send a link to this problem, I want to test a sol...

ur right. solution is this: ```py
import sys
rw = sys.stdin.readline
M = int(rw())
N = int(rw())
choices = set()
K = int(rw())
for i in range(K):
g = input()
if g in choices:
choices.remove(g)
else:
choices.add(g)

numR = 0
numC = 0
s = 0
for choice in choices:
if choice[0] == "R":
numR += 1
elif choice[0] == "C":
numC += 1

for choice in choices:
if choice[0] == "R":
# numR += 1
s += N - numC
elif choice[0] == "C":
# numC += 1
s += M - numR
print(s)

#

moral of the story: searching lists r more expensive than searching sets when it comes to bigger numbers

topaz pulsar
#

what I was planning on was have a set of row numbers and column numbers, then for each input, add or remove that number from the appropriate set depending on if it exists or not (because if the same row is chosen twice it cancels out) then at the end, the number of gold tiles will be M * len(columns) + N * len(rows) - 2 * len(columns) * len(rows) i think, because thats the number of tiles that are gold because of the rows + the numbre of tiles made gold because of the columns, minus the overlap

#

so that would be O(K) time and O(N + M) space

#

i think thats similar to what you did but the only real difference being that final for-loop to calculate s you may be able yo make O(1) using M * numC + N * numR - 2 * numC * numR

vocal gorge
#

@dreamy arrow And if you meant my earlier analysis of that code snippet's complexity - no, it's n!, as the plot proves.

rustic sparrow
#

we have this question for school but none of us can find the logic error, we've been staring at it for half an hour.

vocal gorge
#

the only problem I see is with the loop

#

but it's less a logic error, and more like an undeclared name

#

because, well, response is not set anywhere

rustic sparrow
#

I think they mean it as some sort of snippet from a larger program

#

so it is probably declared somewhere else

vocal gorge
#

That's true, but it's also not changed over the course of the loop

#

So that loop will either not execute at all, if response is not "Y", or it will loop forever if it is.

#

There's no code that allows exiting the loop via X as it states.

rustic sparrow
#

ok, thanks for the help!

#

I found the answer, every time you enter x to exit the loop it adds 1 to rCount anyway so rCount ends up wrong.

vocal gorge
#

lol, so you're not supposed to notice that there's also no input being taken?

agile sundial
#

input is being taken though right?

rustic sparrow
#

type is equal to the input

#

i think

brazen glade
#

Is this a snippet from an existing language or pseudocode?

rustic sparrow
#

pseudocode

brazen glade
#

Okay thanks

agile sundial
#

reverse a doubly linked list 😔

brisk saddle
#

i refuse to believe i just heard someone say "array list"

#

@mystic basin i mean yeah i guess.

agile sundial
brisk saddle
#

nothing that just sounds odd

#

i did not know an "array list" was a thing.

agile sundial
#

it's a class in java lol

brisk saddle
#

🤨

#

it is?

#

what is it?

agile sundial
#

yeah, although it shouldn't really be used

#

it's like a vector

brisk saddle
#

i see

agile sundial
#

like python lists

brisk saddle
#

yeah

#

i thought it was another case of Java just being special, like with Hashtables and Hashmaps being subtly different things (not sure if that is still true)

agile sundial
#

yeah, they're different, but hashtable isn't really used anymore

brisk saddle
#

fair enough.

#

anyway reversing a Linked List sounds wild.

agile sundial
#

doesn't sound too difficult

#

yeah, it's not that difficult

brisk saddle
#

it does not, but as far as i know modifying something as you loop through it is not a good idea.

but maybe it does not involve that, not sure. never tried it.

#

you could potentially just construct a new Linked List, though?

#

that is my first thought.

agile sundial
#

yeah, but i think the challenge is doing it in place

brisk saddle
#

mostly yeah

old rover
#

so does anyone have any good resources to start learning these

#

or books

#

i'm not doing a bootcamp to learn how to do interview questions so anything would be helpful

brisk saddle
#

i do not have any books, but i started with a Linked List, so maybe do some Googling and find out how to implement one.

#

i could quickly explain right now, though.

#

Linked Lists are a pretty important datastructure to understand in order to implement some other datastructures.

#

at least, the idea of "linking" Nodes is.

old rover
#

all i know is the meme about reversing a linked list

brisk saddle
#

so i highly suggest you start there.

old rover
#

ok i will check it out

#

yeah uh i read a couple chapters and my head started spinning

brisk saddle
#

i remember a Crash Course Computer Science video introduced me into Linked Lists and it did a pretty good job of explaining them.

old rover
#

ok

#

and you would recommend doing a lot of problems right

brisk saddle
#

yeah, probably.

old rover
#

i want to create at least one or two portfolio things before i go ham over hackerrank and leetcode

brisk saddle
#

especially if you want to go into interviews for it.

old rover
#

ok great thanks

#

can i bring the hackerrank and leetcode questions here too or should i only ask in help channels

brisk saddle
#

i mean i do not see anything wrong with asking here.

#

just do not ask questions about Euler challenges i believe.

old rover
#

ok great

brisk saddle
#

you can get in trouble for posting answers to those i think.

#

not sure if the staff here enforce that, or not.

old rover
#

pls i can't even do list comprehension bc i've never seen it before

brisk saddle
#

that might be after the one hundredth question, though.

old rover
#

that doesn't have much to do w DSA

brisk saddle
#

List Comprehension is just when you construct a new list based off another iterator.

old rover
#

i can google list comprehension questions and try and solve those

brisk saddle
#
x = [n for n in range(10)]

puts all the numbers from zero to nine in the list.

old rover
#

i don't get what n for n is tho

brisk saddle
#

it is equivalent to this:

x = []

for n in range(10):
    x.append(n)
old rover
#

n for n is just syntax

brisk saddle
#

yeah, it might be better to represent comprehension like this:

#
n = [(expression) (iterator)]
#
x = [n + 5 for n in range(10)]
#

this will put every number zero to nine, add five to it, then put it into the list.

#

the for loop bit is essentially just where you get your variable from, and the expression describes how that variable should be mutated before being put into the list.

#

in this case, we add five to it.

#

but this is off-topic to the channel, so i guess i will stop here.

old rover
#

sorry i asked

#

i'll come back once i work my way up to DS/algo

brisk saddle
#

it is no problem

old rover
#

thank you

brisk saddle
#

anytime

drowsy minnow
#

Is anyone free to just look over an algo solution I created? I got it to run and I think pretty effiecntly but I'm at the point where I want to be able to see how to improve my method of code.

uneven jungle
#

go

#

@drowsy minnow

drowsy minnow
#
def load(wordList):
    trie = {}
    for word in wordList:
        addWordToTrie(trie, word, 0)
    return trie

def addWordToTrie(d, word, idx):
    if idx >= len(word):
        d['leaf']=True
        return
    if word[idx] not in d:
        d[word[idx]] = {'leaf':False}
    addWordToTrie(d[word[idx]], word, idx+1)

def findWords(trie, word, currentWord):
    myWords = set()      
    for letter in word:
        if letter in trie:
            newWord = currentWord + letter
            if trie[letter]['leaf']:
                myWords.add(newWord)
            myWords = myWords.union(findWords(trie[letter], word, newWord))
    return myWords

def main(keypads, wordlist):
    results = []
    minLength = 5
    trie = load(wordlist)
    
    for pad in keypads:
        count = 0
        for word in sorted(findWords(trie, pad, "")):
            if len(word) >= minLength:
                count = count+1
                
        results.append(count)
    return results


wordlist= ['APPLE', 'PLEAS', 'PLEASE']
keypads = ['AELWXYZ','AELPXYZ','AELPSXY','SAELPRT','XAEBSKY']
print(main(keypads, wordlist))
#

You're attempting to solve a puzzle in an escape room with your team.
You need to open a door to get to the next stage.
There are several doors, each with a different keypad on it.
The keypads each have 7 keys, containing 7 distinct letters.
The instructions state that one of the keypads will open the correct door leading to the next stage.
Your job is to find a word that unlocks the correct keypad.
After struggling for some time, the escape room leader gives a clue.
The first letter of the keypad is guaranteed to be in the word that opens the door.
We will call this letter the "key" letter.
The goal of this exercise is to come up with as many words possible that your team can test out on the keypads and find the correct combination to go the next stage of the game.

#

WHAT YOU KNOW:

The correct combination will be a valid english word.
The words are at least 5 letters long, with no maximum length.
The "key" letter will be in the correct answer.
The words do not contain any letters outside the seven letters on the keypad.
Letters may be reused, inlcuding the "key" letter.

For our purposes, we'll express each set of keypad letters as a string of length 7, where the first letter (the one at posistion 0), is the key letter.
An array of integers. Each Integer should be the number of valid words in the correspdoning lock.

REQUIREMENTS:

Implement a function numKeypadSolutions(wordlist, keypads) that follows the function signature below:

INPUT:

wordlist: An array of strings. This is your list of "valid English words" for the purposes of the following puzzles.
keypads: An array of strings. Each string is the sequence of letters on the keypad.

OUTPUT:

An array of integers. Each integer should be the number of valid words in the corresponding lock.

#

CONSTRAINTS:

Both the wordlist and the keypad letters will be supplied in all capital letters.
All words in the wordlist will be of length 5 or greater.
Every sequnece of keypad letters will be of exactly length 7.
Every sequence of keypad letters will consist of 7 distinct letters.

EXAMPLE:
INPUT:
wordlist: ['APPLE', 'PLEAS', 'PLEASE']
keypads: ['AELWXYZ', 'AELPXYZ', 'AELPSXY', 'SAELPRT', 'XAEBKSY']
expected output: [0,1,3,2,0]

EXPLANATION:

None of the words in the wordlist can be formed from the letters in keypad 0.
Only APPLE is valid for keypad 1.
APPLE, PLEAS, and PLEASE are valid for keypad 2.
Only PLEAS, and PLEASE are valid for keypad 3, since APPLE does not contain the key letter "S".
None of the words are valid for keypad 4, since none contain the key letter "X".

#
  • I realize my solution doesn't apply the "key" letter though so that's what I'm attempting to throw in right now but it keeps messing everything up. *
#
  • The expected output should be [0,1,3,2,0] and I get [0,1,3,3,0] since I haven't been able to throw in the if check properly for the index of keypad I think
uneven jungle
#

One thing, is that you can break the test if the tested letter fails, this cuts down the time complexity

fiery cosmos
#

hi

brisk saddle
#

good afternoon

uneven jungle
#

result = [0]*len(keypads)
for word in wordlist:
for keypad in keypads:
for letter in word:
add = False
if letter not in keypad:
break
else:
add = True
if add == True:
result[keypads.index(keypad)] +=1
print(result) @drowsy minnow

#

@drowsy minnow there's an error in the expected output

#

I believe it should be 3 just as you yielded

#

I posted an alternative without new dicts and stuff

drowsy minnow
#

Thanks I will check this out!

#

This is just within the main function correct? I

#

ah yeah okay I see...

uneven jungle
#

you can cut the word length a bit by making a set, but I doubt that operation is faster than just running one extra round

#

if you have mega long words it's better to make them in to sets

vocal gorge
#

though I didn't know Vector was synchronized, so it's more like ArrayList is the normal vector-like thingie in Java

spiral scaffold
#

can binary trees contain duplicate elements?

#

can't tell if they're supposed to be unique or not

brisk saddle
#

@spiral scaffold of course they can

#

if by elements you mean Nodes containing the same value

spiral scaffold
#

huh

#

ok thnx 🙂

brisk saddle
#

no problem.

quaint summit
#

Hello there, I'm trying to make a program that defines the ID in a string and process it. I already made it with a full string (I made a censor algorithm which defines the word that I defined and changes those words into "censored"). But I can't make it with an integer in a string. I can't convert the string into integer in a for loop. Can you help me please?

brave oak
#

find big numbers and remove them?

quaint summit
#

Yes. Exactly.

#

I just want to define the numbers in string.

brave oak
#

use regex?

quaint summit
#

I'm a newbie programmer and I don't know exactly what that is.

brave oak
#

!e

import re

print(re.sub(r'\d{10,}', '<replaced>', 'abc def 123 456 1234567890123 ghi jklmnopqr'))
halcyon plankBOT
#

@brave oak :white_check_mark: Your eval job has completed with return code 0.

abc def 123 456 <replaced> ghi jklmnopqr
quaint summit
#

I'll give it a look

brave oak
#

something like that, yeah

quaint summit
#

Alright, thanks a lot ^^

visual cipher
#

So I hvae an assignment from my teacher to add up quarters, nickels, pennies, etc and get the dollar and cent amount, and then get the dollar and cent amount separately from each other

#

would I just use the split function and split on the decimal? He doesn't say at all how to do it

#

or is there like another way to do it

visual cipher
#

yes

brave oak
#

why would you have a string to split

visual cipher
#

because it would end up to be like $2.38

#

I need to get the dollar and cent separated

visual cipher
#

unless im just tweaking and not understanding this

brave oak
#

think about it

#

this way

#

the various coins

#

are multiples

#

of a cent

#

you add them all up

#

you get a final number.

#

that represents the number of cents

#

in the result

#

THEN you can convert it into other forms

#

one of which is the dollar and cent amount with a dollar sign

brave oak
#

or just get the number of dollars in that amount

#

(int)

visual cipher
#

addOutput = (quarters * 0.25) + (dimes * 0.10) + (nickels * 0.05) + (pennies * 0.01)

#

This is what I have

#

so far

#

It gives me 2.05 with a certain input

brave oak
#

aaaAAaaaaaa

#

snake_case please

#

anyway

brave oak
#

the number of dollars

#

with cents as the fractional part

#

I would suggest

#

you store the number of cents

#

as an int instead

random tendon
#

how to create a data structure and a alogrithm

agile sundial
#

download the latest firmware for your organic neural net

rich veldt
#

Hi everyone soleone know what's the condition in a* Algorithm when f(n) is duplicated!!?

keen hearth
placid lantern
#

Is there an easy way to get the total numbers of unique items over multiple lists? Say I have a list {h, y, e, w} and a list {a, c, h, e} the total number of unique elements would be 6 while the total elements would be 8. The problem is I'm doing this with about 4k items over a couple hundred different lists so I need an efficient way of doing it.

agile sundial
#

4k items total?

#

or 4k each

placid lantern
#

4k items total

agile sundial
#

just throwing them into a set should be fast enough

placid lantern
#

I dont know why I didnt think of that

#

Im so dumb

#

thanks

warm orbit
#

algorithms?

#

I'm struggling with coming up for a cross product between two n-dimensional vectors.
I only have 2-d and 3-d

def cross_product(a,b):
    if a==b or isinstance(a,int)or isinstance(b,int):return(0,0,0)
    if not(hasattr(a,'__iter__')and hasattr(b,'__iter__'))or len(a)!=len(b)or not len(a):return NotImplemented
    if len(a)==2:return(0,0,((a[0]**2+a[1]**2)**0.5)*((b[0]**2+b[1]**2)**0.5))
    elif len(a)==3:
        """| i  j  k |
           | a0 a1 a2|
           | b0 b1 b2|"""
        return(
            a[1]*b[2]-a[2]*b[1],
            b[0]*a[2]-a[0]*b[2],
            a[0]*b[1]-a[1]*b[0]
        )
    else:return NotImplemented
#

is there even such thing as a 4-dimensional cross product?

hard bane
warm orbit
#

which direction would the cross product point in in 4 dimensions, then?

#

what is a cross product in higher dimensions?

teal haven
#

no idea tbh

latent wasp
#

Hey guysI have a list of dicts that is generated with this:

    today = datetime.combine(date.today(), time())
    data_list = []
    time = today

    while True:
        if time.timestamp() < datetime.now().timestamp():
            data_list.append({int(time.timestamp()): {time:time.strftime('%I%p'), 'success': 0, 'fail': 0}})
            time += timedelta(hours=3)
        else:
            break

While I iterate through data_list is there a way I can get the key of each item in the list?
This is how the data list looks

[{1614031200: {'time': '12AM', 'success': 0, 'fail': 0}}, {1614042000: {'time': '03AM', 'success': 0, 'fail': 0}}, {1614052800: {'time': '06AM', 'success': 0, 'fail': 0}}, {1614063600: {'time': '09AM', 'success': 0, 'fail': 0}}, {1614074400: {'time': '12PM', 'success': 0, 'fail': 0}}]
#

Oh never mind I found it:

keys = all_keys = set().union(*(d.keys() for d in data_list))
coral loom
#

👋 Are there any ways I could append values from a current predefined Pandas dataframe to a new column in a new created dataframe?

lone creek
#

it always goes up by one dimension (given that you have enough vectors occuping all the dimensions i suppose)

raw quest
#

So I just got this curve which is the result of a genetic algorithm simulation depending on the selection rate for the TSP : https://media.discordapp.net/attachments/780135902524997642/813810960140992512/SR_0.9_to_1.png

I find really weird what happen near 100% selection rate, is that supposed to happen?
Or do you think it's related to my program?
this was a zoom I made with 10 different selection value btw 0.9 and 1, here is the global curve (with 10 points again) https://cdn.discordapp.com/attachments/780135902524997642/813809866895523911/selection_rate_graph.png

#

I can't see any reason why 100% selection rate get way better results than 98%

vocal gorge
# warm orbit I'm struggling with coming up for a cross product between two *n*-dimensional ve...

@hard bane I believe cross product is only defined for 3d, actually - it's a pretty weird operation in that regard.
In exterior algebra terms, [a,b] is defined as *(a ^ b), where ^ is the exterior product and * the Hodge star operator. Now, * maps k-forms to n-k-forms, and for vectors (a vector is a 1-form) a,b, a ^ b is a 2-form. As a result, *(a ^ b) is a n-2-form, which is a vector (1-form) only for n=3.

#

So you can define the cross product for an artibrary number of dimensions, but in general it takes two vectors and returns an n-2-form. Only in 3d is the result a vector itself.

#

It's also mentioned on Wiki, apparently:
https://en.wikipedia.org/wiki/Cross_product#Exterior_product

As mentioned above, the cross product can be interpreted as the exterior product in three dimensions by using the Hodge star operator to map 2-vectors to vectors. The Hodge dual of the exterior product yields an (n − 2)-vector, which is a natural generalization of the cross product in any number of dimensions.

wary wadi
#

i require a very basic a-star implementation that could be used for older python versions, so i dug through google and found one... but it is broken... (funny thing is, it is actually copied over the years by people trying to educate people on pathfinding)

my math is just not good enough to fix this simple version of a*.. i was wondering if there is a enthusiast out there that can help me out?

https://pastebin.com/VrG34ypR

feel free to tag me if you're interested... thanks!

warm orbit
#

What's an a star?

agile sundial
#

it's a an gaseous ball

#

there's also an algorithm called A*

boreal depot
#

I'm currently I'm using a bubble sort in my project, but it takes FOREVER to sort 10k+ lines in the currently edited file.
Here is my code: https://github.com/ayourk/HostsManager/blob/main/hostsman.py
During the bubble sort, I also check for duplicates and mess with them appropriately. Is there something I could use that is better/faster/more efficient? I don't have direct access to the editor_lines buffer, but I can move around the buffer using positions. This is a tkinter app and it mostly works.

agile sundial
#

is the function in question mnuToolSort

boreal depot
#

It's around line 650

agile sundial
#

gotcha

#

must you use your own implementation?

boreal depot
#

Is there a better way that can also check for dupes?

agile sundial
#

where are you checking for duplicates?

boreal depot
#

Line 679

agile sundial
#

since there's a lot of clutter in your code, and i don't really know what's going on, could you just describe how you're doing that

boreal depot
#

Nested double for loop setup for bubble sort, the Try..except section fetches 2 consecutive lines from the buffer and splits them out into lists. Each list should contain at least 2 elements to be valid for sorting. The 2nd item in the list is compared for sorting and checked to see if it is a duplicate.

austere sparrow
#

https://github.com/ayourk/HostsManager/blob/main/hostsman.py#L649
Here you're doing quite a lot of stuff while sorting, so it's not very easy to see the logic.
I would suggest doing this:

  1. Make a function that takes a list of hosts (as dicts/tuples/your own classes) and turns them into tkinter stuff
  2. Then use the built-in sorted/list.sort to sort the list
boreal depot
#

The user has an option to exclude some lines from sorting. A series of lines can be sorted while leaving the rest of the edit buffer intact.

austere sparrow
#

Sure, but you can do all the transformations with the data on the lists, not hot-swapping the graphical objects.

#

It will be much easier to just work with a list.

boreal depot
#

When loading the file, not all items are qualified to fit into a list. And the hosts file that I'm using is about 70k entries in addition to all the other comment lines, etc.

#

I'm concerned about memory usage a little.

austere sparrow
#

Here you'll basically have to implement an efficient algorithm like mergesort or quicksort on scratch. Which is not very hard, but if you're going to be modifying the graphics on every swap, it's going to be very slow.

boreal depot
#

Merge sort would be my goal for this app as new files could be inserted into the buffer and I'd love to sort them/check for dupes as they are being loaded

austere sparrow
#

Sorting or removing duplicates from a 70k-sized list is going to be very fast.

#

Certainly faster than mutating a bunch of tkinter stuff along the way

boreal depot
#

I just realize that my app is a text editor first, and went that route.

austere sparrow
#

If every entry is 100 bytes, then the entire file will take up about 7 MB, which is less than half the memory needed for an interpreter on its own

#

If you use bytes, that is.

#

If you use str, it's going to be more like 30 MB

boreal depot
#

The lists/dicts still need to be transferred back and forth from the edit buffer though, right?

austere sparrow
#

wdym?

boreal depot
#

Or are you saying for the sort alone, just load everything into a list and then do somelist.sort() and put things back into the edit buffer later?

austere sparrow
#

What is the program supposed to be doing?

boreal depot
#

Open /etc/hosts files that can include comment lines, hosts definitions and in some cases blank lines. In mine, I have a header that is 65 lines long.

austere sparrow
#

So it should take N hosts files, merge them, sort them and remove duplicate lines?

boreal depot
#

Correct, and at the same time, allowing some lines to exist as comments/extra white space outside of the specified sort range.

austere sparrow
#

Why can't that be done with standard unix tools like sed and uniq?

#

and why do you need a GUI?

boreal depot
#

Some hosts lines have comments at the end of them (list length is > 2) and some of them don't. These are treated as unique entries when in fact they aren't. Also, I'm trying to consolidate those comments onto the duplicate host (see line 687).

#

the GUI is there to allow the user to make changes manually.

austere sparrow
#

I see

#

well, the easiest and fastest way to do it would be to split the program into two parts:

  1. a function that applies all the transformations (sorting, filtering out lines etc.) to a list of lines
  2. all the GUI stuff
boreal depot
#

My problem is if I go that route, how do I know which lines/list entries I need to update when the user makes a change? I already know how to detect the modified flag of the edit buffer.

austere sparrow
#

But the user only makes the changes after you've sorted and filtered the lines, right?

boreal depot
#
  1. The user loads the file into the edit buffer
  2. The user optionally chooses to sort/filter the buffer
austere sparrow
#

Okay. So when the user presses the "sort" button, you can read the text from the editor widget, sort it, and then write the new contents back in.

boreal depot
#

Yeah. That's what I'm doing now. I just know my way is not very efficient. I went with bubble sort since that was the sort I could remember back from my CompSci 101 days.

austere sparrow
#

No, you're sorting them in-place in the widget. What I'm suggesting is getting the text as a string, turn it into a list of lines, and sort the list (which is fast).

boreal depot
#

For sorting the list/dict, can I sort based on 1 field contained in that line using a space as a field separator?

#

Each line I use the str.split to at most 3 list entries and only compare dupes on the 2nd entry.

austere sparrow
#

.sort() is like sorted, but in-place

boreal depot
#

so in this case xs is the list to sort? Can lists contain lists? I haven't looked that close at python's data structures yet.

#

If so, then I could break each line down in advance into a list and store each line in its list form.

austere sparrow
#

Are you coming from a C background?

boreal depot
#

Mostly.

austere sparrow
#

Yes, Python lists can contain anything. Basically, a list is just a dynamic array of pointers.

#

!e

users = [
  {"name": "Bob", "age": 42}, 
  {"handle": "@alice", "age": 50},
  {"handle": "@charlie", "age": 19},
]

def key(user):
    return user["age"]

print(sorted(users, key=key))
halcyon plankBOT
#

@austere sparrow :white_check_mark: Your eval job has completed with return code 0.

[{'handle': '@charlie', 'age': 19}, {'name': 'Bob', 'age': 42}, {'handle': '@alice', 'age': 50}]
austere sparrow
#

!e
You might also want to check out dataclass, which is like a C struct

from dataclasses import dataclass

@dataclass
class User:
    name: str
    age: int

alice = User(name="alice", age=35)
print(alice)
print(alice.name, alice.age)
halcyon plankBOT
#

@austere sparrow :white_check_mark: Your eval job has completed with return code 0.

001 | User(name='alice', age=35)
002 | alice 35
austere sparrow
#

(the type annotations don't do anything at runtime, they're just there for documentation)

boreal depot
#

So I could do key=lambda: linekey(x) and make sure in the linekey func to always compare against the 2nd list entry?

austere sparrow
#

yep. or just key=linekey

#

just like you would do in C's quicksort

boreal depot
#

!e

halcyon plankBOT
#

You are not allowed to use that command here. Please use the #bot-commands channel instead.

austere sparrow
#

(it takes a comparison function instead of a key function, but otherwise same idea)

boreal depot
#

sample data:

0.0.0.0 00fun.com
0.0.0.0 00fun.com #[Tracking.Cookie]
0.0.0.0 zzhc.vnet.cn
0.0.0.0 nabijaj-internetowo.eu```
#

So the plan would be to break down each line into a list causing a list such that:

hostentries = [
  ["0.0.0.0", "00fun.com"],
  ["0.0.0.0", "00fun.com", "#[Tracking.Cookie]"],
  ["0.0.0.0", "zzhc.vnet.cn"],
  ["0.0.0.0", "nabijaj-internetowo.eu"]
]```
austere sparrow
#

Yes, that would work. Then you could have a function like

def key(entry):
    if len(entry) < 2:
        return "".join(entry)
    return entry[1]
# or:
def key(entry):
    if len(entry) < 2:
        return "".join(entry)
    host, domain, *_ = entry
    return domain
#

I suppose you won't have any blank lines?

boreal depot
#

Correct, blank lines are considered bad for the sorted range. Also considered bad are where list items are < 2 per line.

#

I've also built in a check to make sure that list_item[0]'s are all equal.

#

And after I'm done, I put the spaces back in for the
" ".join()

#

Forcing invalid lines to the top/beginning of the list would be acceptable.

austere sparrow
#

or maybe just remove them

boreal depot
#

Removing them could make it difficult to do a 1:1 replacement within the edit buffer?

austere sparrow
#

what do you mean?

boreal depot
#

After doing the sort, the edit buffer would need to be rewritten.

#

Then the user can manually edit after the sort completes.

austere sparrow
#

you mean, the user won't have the chance to rearrange the lines if you remove them?

boreal depot
#

reducing the number of lines will be problematic for the dialog I built. I did build in a failsafe to recheck the number of lines and update the EOL range so it isn't impossible. I think it might be best to at least bring the bad lines to the top of the range so that the user can manually delete those lines if he/she wishes.

#

Unless they are dupes of course. But dupe handling could be handled in 1 pass after the sort completes too.

austere sparrow
#

Standalone comments and blank lines don't make much sense if you sort the lines, right? Because now there isn't necessarily a block to which the comment applies

boreal depot
#

That is why I have that beautify filter that I'll be incorporating in the selected line range before the sort. Most of that will already be done. Now if there is an easy way to combine 5 regexp's into 1, I'd really love that.

austere sparrow
#

what do you mean by combining regexes?

boreal depot
#

Lines 583, 595, 607, 618, & 633 are where I filter out bad stuff from a user selected range with spinboxes.

#

I have to use TCL regexp's here because of tkinter

austere sparrow
#

You could use |, right?

boreal depot
#

I delete text matching those regex's

austere sparrow
#

AFAIK, when reading from a file Python converts all line endings to \n, but that's more of an aside

boreal depot
austere sparrow
#

Well, I don't know much about Tcl/Tk, so can't really help here. Maybe ask in #user-interfaces.

boreal depot
#

I've tried there. Last time I asked there I got crickets

#

I'll try that | operator and do some tests.

austere sparrow
#

if that doesn't work, you can just use Python's re module

boreal depot
#

That means going back and forth with the edit buffer 😾

#

I'll deal with that with the sort since it is an overall improvement.

austere sparrow
boreal depot
#

I've been reviewing some of those docs. I learn best by examples though.

austere sparrow
#

anyway, I'll go to sleep now

boreal depot
#

thanks, sleep well

austere sparrow
boreal depot
#

I haven't much success with them either.

#

These topic specific channels have been fruitful though (most of the time).

austere sparrow
#

Also, https://docs.python.org/3/tutorial/index.html is a pretty dense introduction to Python, and you can pick the sections you want. Not very suited for total beginners IMO, but good if you are already familiar with programming.

boreal depot
#

I think I've progressed a bit beyond that.

hearty badge
#

I'm trying to do this problem https://open.kattis.com/problems/almostperfect
This is my solution but it's too slow. What could i do to make it faster? I'm kinda new to python and only read about generators and yield keyword. Would that make it significant faster or do i need to rethink my approach to the problem?

import sys
x = sys.stdin.readlines()


for line in x:
    n = line.strip('\n').split(' ')
    n = int(n[0])
    result = 0
    for i in range(1, n):
        if n % i == 0:
            result = result + i
    if result == n:
        print(f"{n} perfect")
    elif (n - result) * 1 <= 2:
        print(f"{n} almost perfect")
    else:
        print(f"{n} not perfect")
glossy breach
#

You only have to check integers from 1 to sqrt(n)

#

If you find 1 divisor of n, let's say i in the range[1, sqrt(n)] then there's a corresponding divisor to i in range[sqrt(n), n] which is n / i

#

So the complexity would be O(sqrt(n)) instead of O(n) like you're doing

brisk saddle
#

alright so i am implementing MergeSort, and i am going to see if i am understanding this correctly.

is it incorrect in describing this as:
while both lists are not empty, pop off the first elements of both, and compare them. append them to their correct list.

then, do the same thing again for both lists, since they might have leftover elements if they are not of the same length.

hearty badge
#

@glossy breach Alright thank you so much! Will try it out!

brisk saddle
#

alright, thank you.

pearl trout
#

is it just me who is taught that python is slowest while solving problems or its just the py algo which sucks that creates a TLE, be my guest and please make me understand it properly

agile sundial
#

for competitive programming, python is very very slow, compared to other choices liike cpp

#

for example, you might have to write an O(n log n) algo in python, while an O(n^2) might work in cpp

brisk saddle
#

thoughts?

agile sundial
#

sure

balmy siren
#

If nodes in the tree (which aren't at height 'h') are filled with both the children then, does it mean the tree is called complete binary tree ?

dapper sapphire
brisk saddle
#

i have implemented QuickSort, as well.

#
def quicksort(source: list) -> list:
    if len(source) <= 1:
        return source

    # Divide
    source_copy = source.copy()
    pivot_index = round(len(source) / 2)
    pivot = source_copy.pop(pivot_index)

    # Re-arrange
    greater, lesser = [], []

    for cmp_val in source_copy:
        if cmp_val >= pivot:
            greater.append(cmp_val)
        else:
            lesser.append(cmp_val)

    # Conquer
    greater = quicksort(greater)
    lesser = quicksort(lesser)

    # Merge
    return greater + [pivot] + lesser
#

it works perfectly fine, but does anyone see any issues with my implementation?

#

i did not get this completely wrong, did i?

brisk saddle
#

@brave oak hey, would you mind giving me a hand here?

#

if you are free, at all.

#

@balmy siren a "complete" binary tree means that each node has either zero, or two children.

#

oh wait, i might be confusing "full" with complete here.

#

A complete binary tree is a binary tree in which every level, except possibly the last, is completely filled, and all nodes are as far left as possible.

#

this is what a "Complete" binary tree looks like.

#

🤨

balmy siren
#

Yeah that's actually a strict binary tree, where each node has either two children or none

brisk saddle
#

i mean.. does a non-strict binary tree allow for one child as well, then?

balmy siren
#

Yes as long as it's not a "full or perfect"

brisk saddle
#

complete, full, perfect, jeez.

balmy siren
#

In "full or perfect" binary tree, each node should have two children and all the leafs are at same level

brisk saddle
#

ah alright, got it. i probably cannot answer your question then. sorry. =(

balmy siren
#

No worries 👍

brisk saddle
#

yeah i was a bit confused at that as well.

balmy siren
#

But afaik, those nodes which aren't at height h (in the picture you mentioned A, B, C, D, E) should have either None or both the children..

brisk saddle
#

it works perfectly fine, but does anyone see any issues with my implementation?
i did not get this completely wrong, did i?

#

i think i followed the description on Wikipedia pretty well, but another set of eyes is always nice in a situation like this.

lean dome
brisk saddle
#

thinkin alright, sick.
thank you!

main bison
#

it does not account for repeated values in the pivot, is that needed?

brisk saddle
#

repeated values in the pivot? i think i saw something on the Wikipedia page relating to that.

#

one moment.

lean dome
#

Ah, the return at the end should have lesser on the left and greater on the right

#

Otherwise you're returning the list in reverse order

brisk saddle
#

alright. i will fix both of the things you brought up.

#

With a partitioning algorithm such as the Lomuto partition scheme described above (even one that chooses good pivot values), quicksort exhibits poor performance for inputs that contain many repeated elements. The problem is clearly apparent when all the input elements are equal: at each recursion, the left partition is empty (no input values are less than the pivot), and the right partition has only decreased by one element (the pivot is removed).

#

@main bison is this what you were referring to?

main bison
#

no, im refering to your code

#

pivot = source_copy.pop(pivot_index)

#

this is one value

brisk saddle
#

my thought process was so i could do this:

return lesser + [pivot] + greater

i originally had it saved as the index of the pivot, rather than the value itself.
it just saved a line or two of code.

#

does that answer?

lean dome
# main bison this is one value

That seems fine to me. It removes the item with that index from the list, leaving any other items with the same value alone. Those will get put into greater when it loops over the list

brisk saddle
#

^

#

it is so the loop for separating lesser and greater elements does not include the pivot itself.

#

sorry, i should have mentioned that.

#

i am not sure if that is 100% needed, though.

#

it was just something i added as a precaution.

main bison
#

what if there are 100 values of the pivot at once?

brisk saddle
#

i can test that, one moment.

#

seems to work fine, was the 100 pivots bit hyperbole, or did you actually mean one hundred pivots?

lean dome
#

If there's a problem with duplicate pivots, I'm not seeing it.

brisk saddle
#

well, the only way to make sure is to test it a bunch of times.
i will just to make sure.

thanks, you two! if you notice anything else i have done wrong, let me know.

#

also i tested it with one hundred two's just to be sure.

#

seems to be working fine to me.

main bison
#

well.. i guess it will be cought in the right hand recursion

brisk saddle
#

yeah, it will.

main bison
#

and then empty sorted left..

brisk saddle
#

left[:center] will go up before the pivot, while right[center:] will include the pivot, and everything else after.

lean dome
#

Right. It chooses the pivot naively, and won't have the best possible performance, but it does sort correctly.

brisk saddle
#

i am not looking to optimize performance (yet, of course).

#

but yeah, it does seem to work.

main bison
#

i would just get all the pivots at once

#

and put them in the middle like your doing

#

since you already know where they should be

lean dome
#

All the adjacent ones, you mean?

main bison
#

if your pivot is 7

brisk saddle
#

^^
is the issue with repeated pivots ones that come one after another, or just multiple occurrances of the same pivot?

main bison
#

and you have five 7s in your list

#

when seven is picked as a pivot the first time, then you know that all the sevens should be in the middle

lean dome
#

Ah, you mean with 3 lists and an if </elif ==/else.

main bison
#

quicksort only sorts the pivot

#

so since it only sorts the pivot in the correct place, you should do that sort

brisk saddle
#

oh you are right.

main bison
#

yes, 3 lists

#

greater, equal, less

brisk saddle
#

this seems more of a performance one, but if there are a lot of the same element than it would be a great increase to performance to just collect all of the same elements as the pivot before the actual sorting begins.

#

if i am interpreting what you said, correctly.

lean dome
#

It's an interesting one. It speeds things up a lot in the case of many duplicate elements, but slows things down slightly in the case with all unique elements

main bison
#

yes, creating lists is very slow

#

I mainly use quicksort as an example to teach recursion

brisk saddle
#

i think i will still do what eivl suggests here, even if it will be a bit slower.

main bison
#

not optimisation of quicksort

lean dome
#

Overall, it's definitely a sensible optimization.

brisk saddle
#

either way, thank you two very much for the suggested improvements.

main bison
#

want me to share my version of it?

brisk saddle
#

yeah sure.

main bison
#
import random

def quick_sort(list_of_numbers):
    if len(list_of_numbers) <= 1:
        return list_of_numbers
    pivot = random.choice(list_of_numbers)

    less_then = [n for n in list_of_numbers if n < pivot]
    equal = [n for n in list_of_numbers if n == pivot]
    greater_then = [n for n in list_of_numbers if n > pivot]

    return quick_sort(less_then) + equal + quick_sort(greater_then)
brisk saddle
#

list comprehension
that is genius

#

alright cool, much appreciated either way.

main bison
#

it also picks the pivot at random instead from the middle. but thats just a variant

lean dome
#

Well... Comprehension is shorter for lines of code, but slower as well

brisk saddle
#

oh right, i remember seeing that different ways of choosing the pivots have their own pros and cons.

#

i will also look into that tomorrow.

lean dome
#

3 loops over the list is slower than 1 loop over it.

brisk saddle
#

this is true

lean dome
#

Slightly.

main bison
#

yes, but it nicely showcases recursion

brisk saddle
#

alright, well i will be heading to bed now.

#

have a nice sleep or morning, wherever you are.

main bison
#

😄 morning here 😄

brisk saddle
#

well, i hope you have a great morning!
goodnight, everyone.

lean dome
#

I should go to bed too 😅

dapper sapphire
#

Hi wondering if someone can look at the below implementation of the remove method for linked list and I also have few questions commented

    def remove(self, targetData):
        current = self.head
        previous = None
        found = False
        while(current != None):
            if(current.data == targetData):
                found = True
                break
            previous = current
            current = current.next
        if(found == True):
            if(previous == None):
                # So in this if statement we only have to do 1. Below are my comments
                self.head = current.next    # current.next is None
                self.head = None            # can we also do this?
                # So would either of the above be correct?
                
                # why cant we do the below? since current.next points None
                # why do we have to do self.head = current.next
                current = current.next
            else:
                previous.next = current.next
#

why cant we do:

            if(previous == None):
                current = current.next

When we do this it doesnt remove the last element

flat sorrel
halcyon plankBOT
#

@flat sorrel :white_check_mark: Your eval job has completed with return code 0.

b = [1, 2, 3, 4] did not modify the value of a
flat sorrel
#

Similarly, the statement current = current.next doesn't change the object referred to by self.head. Instead, it causes current to reference current.next without doing anything to self.head.

dapper sapphire
#

Oh ok, I see what you mean, current in that situation does not reference to self.head.

#

So we have to explicitly mention self.head and then point it to None.

sage berry
#

i was wondering how i could make a basic text generator based on seeds

brisk saddle
#

hey i remember that

#

@sage berry my first thought? a two-way/reversible hash algorithm.

#

now, do i know any that you could apply to your situation? no, but it might give you a better idea on how to go further.

#

if you do not know, a Reversible Hash Function is essentially just a Hash algorithm that can be, well, reversed, to get the original input.

#

the hash you initially generate would be the seed, and you can throw text through hashing algorithm to retrieve it's index in the sequence.

sage berry
#

hash is a java term right? isnt it the equivalent of a dictionary in python?

#

or is it also some kind of encoder

brisk saddle
#

Hashing is used in Hashtables/Hashmaps, but it is not exclusive to Java.

#

Hashing is when you take one input, and then turn it into something else. (bit of an oversimplification, i suppose.)

#

in the world of Computer Science this is often used to convert values to numbers, like in the case of a Hashtable, but this is certainly not the extent of the use.

#

so yeah, i guess you could say it is like an encoder, i suppose.

sage berry
#

ok, so i need to make a reverseable encoder

brisk saddle
#

yup!

#

now, how you do that is up to you, though.

there are one or two things that you need to look out for. for example, not only does your Hash algorithm have to be reversible, it will also have to be "perfect."

#

well, if you do not want collisions, i guess.

#

it does not have to be, but a non-perfect hash function can produce two values that return the same hash.

agile sundial
#

hashing algorithms are meant to not be reversible

sage berry
#

you mean i cant have 2 items be encoded to the same thing

brisk saddle
#

they are not? 🤨

#

you certainly can, but i do not see why you would.

agile sundial
#

how would reversing a hash even work

sage berry
#

its an encoder not a 'hash'

brisk saddle
#

good question, it is just an idea that came to mind.
sorry if i confused it with something else.

sage berry
#

the first problem i run into is the encoding

#

if i have the seed set to 0

#

how do i take that and generate 410 pages or whatever

#

in your quora article it said something about a 273 bit hash

#

does that mean that the output is 273 bits

brisk saddle
#

i mean, if you wanted to hash for example a string, and did so by adding up the ordinals of it's individual ASCII characters, you would need a way of extracting the individual ordinals out from the resulting number, i suppose.

agile sundial
#

*happens to be very difficult

brisk saddle
#

yes, so i guess if you wanted to "reverse" it you would somehow need to attach other information.

sage berry
#

there has to be a way to decode stuff or else all of cryptography is a joke

agile sundial
#

yes, it's encryption and decryption, not hashing

#

hashing is one way

brisk saddle
#

ahh yes, that is the right term, sorry.

sage berry
#

ok so we need to make an encryptor and decryptor

#

i not we

brisk saddle
#

yeah, you do.

sage berry
#

which should be simple? its supposed to be bad

brisk saddle
#

i suppose it depends on how you plan on having your seed be represented.
entirely numerically? base-64?

sage berry
#

yeah i think a base 10 seed of numbers

#

why do i need anything else

brisk saddle
#

shrug, some people just choose other methods.

sage berry
#

when someone says 256 bit encryption that means the encrypted message is always 256 bits

brisk saddle
#

i believe so, yeah.

sage berry
#

1 character is a byte right

agile sundial
#

what do you mean by character?

brisk saddle
#

not necessarily.

sage berry
#

like 'a','b'

brisk saddle
#

ASCII characters, yes.

agile sundial
#

they are 2 bytes, i believe

brisk saddle
#

if you include the extended table, it is one byte.

sage berry
#

oh is the 265 bit encoded message a number that i convert into base 26 and use letters?

brisk saddle
#

if you mean the standard table, it is 7 bits, but i think it is still stored in one byte.

#

i believe some Unicode characters are actually multi-bytes.

agile sundial
#

yes

brisk saddle
#

yeah, so if you intend to allow for Unicode, that might be a bit of an issue if you use UTF-8 as your encoding.

#

i might be wrong there, though.

sage berry
#

then how do you decode 256 bits into more than 32 characters

brisk saddle
#

that.. is a good question. frankly i am not educated enough in cryptography to answer.

sage berry
#

maybe is does it in 32 character blocks

#

ok yeah they break down the message into blocks of 4 bytes or whatever

opaque spruce
#

I'm confused

#

can someone help

opaque spruce
#

im done already lmfao

dapper sapphire
#

!e

print("How does this work??")
halcyon plankBOT
#

You are not allowed to use that command here. Please use the #bot-commands channel instead.

dapper sapphire
halcyon plankBOT
#

You are not allowed to use that command here. Please use the #bot-commands channel instead.

fickle seal
#

Can anyone join my team in hashcode?
I have zero experience and no team pls help anyone

cloud moth
#

do anyone know what data structures to look for building charts/graphs ?

carmine flare
#

Think that depends on the chart and graph probably?

trim fiber
#

No, what is this?

trim fiber
#

However you can use matrix if there are many edges between nodes

queen dagger
cerulean wing
#

found a course online, any feedback

brisk fox
#

Create the following Application.

Use for loop, while loop, and do while loops to generate a table of decimal numbers,
as well as the binary, octal, and hexadecimal equivalents of the decimal numbers,
in the range 1-256.

The program should print the results to the Console without using any of the python conversion functions.
You must design and implement the necessary algorithms for the conversion.

Make sure to ready the requirements, ask question if not sure about the requirements.

Design Details:

The Main program should prompt the user for input.
A sample output is shown below.

Note:
To generate the binary numbers your code should use a while loop to generate
the binary numbers, a “for” loop to generate the octal numbers and a “do .. while” to
generate the hexadecimal numbers.

#

How do I do this?

#

I dont understand how the while loop is used to calculate the binary numbers

#

especially given that the numbers are a range

glass steeple
#

not sure if this is more for this room or the data science room.i have a question about cleaning my csv data...i have this columns
Date,open,high,low,close,Volume BTC,Volume USDT,tradecount
2 0,2021-02-23 00:00:00,54087.67,54183.59,53405.5,53414.59,572.09326,30786788.62840657,16645.0

how do i clean the 0 column before 2021-02-23 but without cleaning the "date" header?

livid moss
runic stream
runic stream
#

I think I may have overcomplicated the question though

livid moss
runic stream
brave oak
#

just generate a join key for the second CSV based on the most recent date from the first CSV that is not later than the corresponding row’s date

#

then merge on that

mortal ravine
#

I am having a problem that needs help in NLP.
The text "TSLA is going to the moon. I think TSLA is the greatest company ever and GM and other car manufacturers don't stand a chance when competing with TSLA" would ideally return something indicating that TSLA had positive sentiment and GM had negative sentiment.
How can I write a code in python?

fiery cosmos
#

helllo

#

ciao

#

i quite the serveur

mint jewel
#
for q in range(-s, s + 1):
    for r in range(-s, s + 1):
        if abs(q + r) <= s:
            print(q, r)
``` how would you write this such that the `if` isn't there. It isn't all that needed, since over half of the pairs are correct, but just out of curiosity.
uneven jungle
#

@mint jewel sth like one Range from -s,0 and the other from 0,s

trim fiber
#

Are you sure that it is {4} and {5} - not {3} and {4}?

#
"There is {0} and {1} but I do not like this {2}. I also know how to do {3} but {4} doesn't let me".format('apples', 'bananas', 'fruit', 'coding', 'someone')
#

!e

print("There is {0} and {1} but I do not like this {2}. I also know how to do {3} but {4} doesn't let me".format('apples', 'bananas', 'fruit', 'coding', 'someone'))
halcyon plankBOT
#

You are not allowed to use that command here. Please use the #bot-commands channel instead.

trim fiber
#
"There is {0} and {1} but I do not like this {2}. I also know how to do {3} but {4} doesn't let me".format(*data)
mortal ravine
#

I am looking for help for my test assessment in NLP / sentiment analysis.
Task: The text "TSLA is going to the moon. I think TSLA is the greatest company ever and GM and other car manufacturers don't stand a chance when competing with TSLA" would ideally return something indicating that TSLA had positive sentiment and GM had negative sentiment.

karmic phoenix
#

switching back and forth from c++ to python is a trip

#

its like speaking english and chinese bilingually to two people all day

#

if you dont do it in python theres a good chance you do it in c++ lmao

uneven jungle
#

@mint jewel Because s-s is <=s for all positive integers.

#

And 0

#

So you need to Only select negative s

#

In the ranges

mint jewel
#

wouldn't that exclude that say 1 1?

uneven jungle
#

1-1<1

#

At least this way you don't need the if.

brave oak
#

@fervent saddle no like a tree dict

fervent saddle
#

I’m thinking more about it, and maybe the trade off would actually be that you couldn’t delete from it at all

fervent saddle
#

It might be complicated to update the hash array which points to insertion ordered array

#

But maybe I’m just not thinking about it hard enough

#

If you deleted, and then you moved everything back in the insertion ordered array, you would have to update the hashed into array so it didn’t direct to the wrong indexes

#

Well, no, maybe it would be fine. It would still be O(n), it would just be a longer O(n), I think

#

And it might need more space

#

Like everything in the insertion order array would need to hold what part of the hashed into array directs to it, or something like that

buoyant badger
#

who can hop on a call and check this

vital compass
#
if int(outlist[counter][0]) + 1 == int(z[0]) or int(outlist[counter][0]) - 1 == int(z[0]) or int(outlist[counter][1]) + 1 == int(z[1]) or int(outlist[counter][1]) - 1 == int(z[1]):

how do i check if this condition is met 3 timmes

brisk saddle
#

have you heard of variables, lad?

#

that code is super messy and does not have to be that long.

#

also just store the result in an array, and then run the all function on said array.

dapper sapphire
#

hahaha

#

You could probably use a counter if the statement is met and increment it to see if it ran 3 times at the end.

dapper sapphire
#

So when we have

    def add(self, newData):
        newNodeAddress = Node(newData)
        newNodeAddress.next = self.head
        self.head = newNodeAddress

newNodeAddress.next is actually the memory address of the previous node
so when do add(1), add(2), add(3)

Below is what's happening. Of course we are dealing with memory address, but I used values to understand what's happening

add(1)
newNodeAddress.data = 1
newNodeAddress.next.data = self.head(None)
self.head = newNodeAddress.data(1)

add(2)
newNodeAddress.data = 2
newNodeAddress.next.data = self.head(1)
self.head = newNodeAddress.data(2)

add(3)
newNodeAddress.data = 3
newNodeAddress.next.data = self.head(2)
self.head = newNodeAddress.data(3)
#

And we assign self.head to newNodeAddress.next first, so we can keep track of the previous memory address

#

Because when we add 1, 2, 3
List will end up looking like 3, 2, 1, where:
self.head.data = 3
self.head.next.data = 2
self.head.next.next.data = 1
self.head.next.next.next = None # There's no data

nova holly
#

guys O(n!) means that it grows very fast even if n is small

#

example:
1 --> 90
2 --> 890
3 --> 21345

#

right ?

jolly mortar
#

yes, O(n!) grows very fast

pallid coral
#

n!:
1 -> 1
2 -> 2
3 -> 6
4 -> 24
5 -> 120
6 -> 720
7 -> 5040
8 -> 40320
9 -> 362880

#

As you can see it gets bad really quickly

#

For an O(n!) operation on a list with 63 items, it would take 1982608315404440064116146708361898137544773690227268628106279599612729753600000000000000 operations

jolly mortar
#

(minor correction: 2! is 2)

lament acorn
#

How can we difference between the caracteres and the integers in python?

undone ingot
#

How can you convert a string into a variable?

fair shell
undone ingot
#

thx

#

I am getting a "unreacheable code" when I want to do more than 2 elif statesments

#

What can i do?

undone ingot
#

@brave oak k = "name1n"

if k == "name":
print("hi")
elif "pter":
print("hi")
elif "d":
print("hi")

undone ingot
#

pycharm says to the last elif "The code is unreacheable

brave oak
#

!e

print(bool("pter"))
halcyon plankBOT
#

@brave oak :white_check_mark: Your eval job has completed with return code 0.

True
sleek pendant
#

My PC from 1 million coinflips throw 16k, 6 streaks

undone ingot
#

@brave oak but when want to print it at the end i doesn't work

#

@brave oak the variable k

hushed rock
#

Hey, I have a question.
I want to make a class that helps with encrypted files for my project in a way that you can decrypt it with a key and edit the content only (the file is supposed to contain a 512bit salt at the beginning and content comes right after it).
Any way to make it work like a buffered reader or BytesIO?
BytesIO(file.read()) will not work because it takes a very long time to get a large file...

Thanks in advance!

dapper sapphire
# hushed rock Hey, I have a question. I want to make a class that helps with encrypted files f...

I havent done a project on encryption and decryption, but I did a project on hashing(md5, and sha2) and recoded part of openssl which I suppose has a similar concept, but you just cant decrypt it. But the code I wrote took longer to run on big files, but the original openssl created a hash pretty much right away even for big files. So I would say if there's an open source project that's doing something similar to what you are doing and that project runs fast then maybe look at the source code and see how they optimized it.

dapper sapphire
#

What's the size of your buffer right now?

#

I think increasing the buffer size or chunk size when you stream data might work.

hushed rock
#

currently my buffer is 32k

dapper sapphire
#

32,000?

hushed rock
#

yeah

#

but like... kb

dapper sapphire
#

Yeah, and how big is the file you are working with is it in GB?

hushed rock
#

I'm testing it on a 6GB file which takes about 5 sec to read whole

dapper sapphire
#

Maybe try increasing the buffer from 32k to 64k and see if it takes less time.

hushed rock
#

maybe, ima try that rq

#

huh... although i think that happen because this time i ran the code on an hdd

#

also seconds not ms

dapper sapphire
#

Yeah run it on the same drive you were previously running it on that gave you 5 seconds with 32k buffer.

#

hdd would be slow.

hushed rock
#

yeah much better

#

welp i think this'll be fast enough for me, I'm not expecting larger than 20gig files

dapper sapphire
#

Nice! And you probably already know the trade off of doubling the buffer size.

hushed rock
#

yeah lmao
thanks!

dapper sapphire
brisk saddle
#

can tombstones in Hashtables be overwritten, or must they remain until the next rehash?

kindred whale
#

How can I insert a node after a prev_node in a Linked List, so basically the input of the function is

def insert_value(self, prev_node, new_value):
    
brisk saddle
#

save the next_node of prev_node in a variable.

#

then, set the next_node of prev_node to Node(new_value) (or however you initialize a Node)

shut dragon
#

hello

brisk saddle
#

then, set the next_node of that new node to the node you saved in a variable.

#

greetings, friend.

shut dragon
#

pip install pyaudio
File "<stdin>", line 1
pip install pyaudio
^
SyntaxError: invalid syntax

brisk saddle
#

this is a channel for algorithms and datastructures.

shut dragon
#

Greetings

brisk saddle
#

you need to run that command in your shell, not the Python interpreter.

shut dragon
#

how can I do it

brisk saddle
#

by executing it in your shell. open your terminal and type that command. you are running it in Python, which is the wrong place.

shut dragon
#

Thank you so much, I'm doing artificial intelligence.

brisk saddle
#

good luck!

shut dragon
#

thanks

brisk saddle
#

no problem.

kindred whale
brisk saddle
#

anytime.

sudden rover
#

but its already on

#

microsoft store aaps does not work

#

can anyone help

night plank
#

hello everyone

#

i got a question about newtwon raphson algorithm

#

can someone help me with this equation pls

topaz pulsar
#

you don't define epsilon anywhere, you also need to indent the relevant part below the while loop so that it is included in that loop

old rover
#

hi everyone i'm a noob to big O

#
x = 5 + (15*20)
y = 15 -2
print(x+y)
#

can someone please explain why constants are O(1)?

mint jewel
#

if the duration is independent of some input size, then it is constant

old rover
#

so any equation with constants is just O(1)

mint jewel
#

yes, 1 + 1 will take equally long regardless of what input you have, since it doesn't use the input in any way

old rover
#

uhhhh should i be learning this before i learn recursion and polymorphism?

agile sundial
#

time complexity?

old rover
#

yes

jolly mortar
#

you should tbh, it's usually intuitive any way

old rover
#

isn't recursion when a function calls itself

#

never used it before

jolly mortar
prisma moth
#

The total number of steps T(n) wouldn't be T(1) but O() abstracts constant factors and only keeps the most important factor

old rover
#

why is a for loop big O(n)

#

sorry i'm asking noob questions

#

learning this for the first time

mint jewel
#

because looping over 500 elements is twice as fast as looping over a 1000 elements

#

the time it takes is linear to the amount of elements

jolly mortar
#

yeah

mint jewel
#

now, this is not true for every for loop

jolly mortar
#

if there was another loop within the outer loop, which is iterating over the list again, you'd get a O(n^2)

#

like

for i in my_list:
  for j in my_list:
    print(i == j)

for each item in the list, it has to go through the entire list again

old rover
#

is there a book that just teaches big O(n)? simply?

agile sundial
#

i doubt you'd find a book with only big O, but any algos text will have something on it

peak wagon
#

Might help to visualize it

old rover
#

i'll watch some more videos and keep asking questions

#

If you've never covered big O in an academic setting, you can probably skip this subsection. It might confuse you more than it helps. This "FYI" is mostly here to clear up ambiguity in wording for people who have learned big O before, so that they don't say, "But I thought big O meant..:'

agile sundial
#

that's from a book...?

old rover
#

cracking the coding interview

#

some edition that I borrowed

jolly mortar
#

pick another book which does explain big O ¯_(ツ)_/¯

old rover
#

yeah i think there has to be one

#

um

agile sundial
#

CLRS introduction to algorithms

old rover
#

do i have to reteach myself calculus to learn big o

#

is that a thing

raw zealot
#

Guys, there is an interview ques
if you are given some coordinatesFind the number of rectangles which can be formed,What's the key to start coding regarding this prompt

#

Like coordinates on a Cartesian plane

#

6-8

#

We have to find all the possible rectangle we can frame using any 4

#

This is a Google interview ques 👀

old rover
#

that looks like an interview question for FAANG

#

damn beat me to the punch

raw zealot
#

Yesh

#

Baang for faang

old rover
#

but it's also a question that startups have started asking too

#

every company wants to be FAANG

#

sorry didn't want to go off on a rant

mint jewel
#

I think you can do this with set intersections

#

but I am not quite sure

old rover
#

so if you have a nested for loop it's O(N^2)?

#

that actually makes sense

prisma moth
#

An O(n^2) algorithm will always be worse than an O(n) in the long run but if the two algorithms are T(n^2 + 1) and T(100n + 1000), then the O(n^2) algorithm is actually better for n < 100

jolly mortar
raw zealot
old rover
mint jewel
#

do the rectangles have to be axis aligned?

raw zealot
#

Yes

#

Parallel to the axis

#

Like

.         .            .

.         .            .
agile sundial
raw zealot
#

These are the points through which 3 rectangles could be formed

mint jewel
#

then you can find all points with common X coordinates and all points with common Y coordinates, and then there is some clever way to do the intersection such that you get all the points

raw zealot
#

Hm

agile sundial
#

if n is small just brute force 😔

raw zealot
#

We need to find the combinations

#

for all 3 rectangle

raw zealot
old rover
mint jewel
#

ye, nothing wrong with a good ole

for a, b, c, d in combinations(points, 4)
agile sundial
#

lul

raw zealot
#

Will try this soon

mint jewel
#

actually, what is the big O of choose k

uneven jungle
#

Question: does this have a particular name? It's not fully regular bubble-sort.

raw zealot
#

Wut

mint jewel
#

well, it is wrong regardless. It can easily go into n < 0@uneven jungle

#

and well, n < -len(S)

old rover
#

what does "worst-case scenario" even mean?

#

"What if we get really unlucky and the pivot is repeatedly the biggest element in the array?"

#

what's a pivot? never heard that term before

agile sundial
#

quicksort?

mint jewel
#

consider bubble sort. Best case scenario is that the list is already sorted, so you don't have to actually swap anything

old rover
#

are you guys talking to me?

mint jewel
#

worst case is if it sorted, but in the wrong direction IIRC

#

yes

agile sundial
#

for bubble sort yeah, reverse sorted is worst case

old rover
#

idk what quicksort or bubblesort even is

mint jewel
#

oh

#

then lets make a simpler example

agile sundial
#

you're in the sorting algos section of your book right?

old rover
#

i'm in the big o notation section of cracking the coding interview

agile sundial
#

oh damn, they assume you know a bunch of stuff

#

maybe start with a more introductoory book then

old rover
#

"O (big 0): In academia, big O describes an upper bound on the time. An algorithm that prints all the values in an array could be described as O(N), but it could also be described as O(N2), O(N3), or 0(2N) (or many other big O times). The algorithm is at least as fast as each of these; therefore they are upper bounds on the runtime. This is similar to a less-than-or-equal-to relationship. If Bob is X years old (I'll assume no one lives past age 130), then you could say X .$. 130. It would also be correct to say that X .$. 1, 000 or X .$. 1,000,000. It's technically true (although not terribly useful). Likewise, a simple algorithm to print the values in an array is O(N) as well as O(N3 ) or any runtime bigger than O(N)."

#

this is like the first paragraph they even show about big O

#

ok cracking the coding interview is overrated

#

for beginners yes

prisma moth
#

Worst case scenario is the max number of operations required

old rover
#

constants matter if the input size is small

#

got it

mint jewel
#

in which case, big O doesn't

uneven jungle
agile sundial
#

you tested that?

mint jewel
#

I tested it and it spat an index error at me.

#
In [33]: S = [random.random() for _ in range(100)]

In [34]: l = len(S) - 1
    ...: n = 0
    ...: while n < l:
    ...:     if S[n] > S[n+1]:
    ...:         S[n], S[n+1] = S[n+1], S[n]
    ...:         n -= 1
    ...:     else:
    ...:         n += 1
    ...:
---------------------------------------------------------------------------
IndexError                                
      2 n = 0
      3 while n < l:
----> 4     if S[n] > S[n+1]:
      5         S[n], S[n+1] = S[n+1], S[n]
      6         n -= 1

IndexError: list index out of range
old rover
#

uhhhhhh

#

there has to be something that can explain big O to my beginner ass

#

trying this one

agile sundial
#

clrs algorithms is more introductory for algorithms in general

old rover
#

you would recommend the third edition right?

agile sundial
#

¯_(ツ)_/¯

#

whichever yuo can get is fine

old rover
#

whatever i found it in the third edition so i'm using it in the third edition i don't know why i asked

#

thanks for suggesting it

uneven jungle
mint jewel
#

it does work on some arrays

#

but not on all of them

agile sundial
#

!e

from random import random
L = [random() for _ in range(200)]
n = 0
l = len(L) - 1
while n < l:
  if L[n] > L[n + 1]:
    L[n], L[n + 1] = L[n + 1], L[n]
    n-= 1
  else:
    n += 1
halcyon plankBOT
#

@agile sundial :x: Your eval job has completed with return code 1.

001 | Traceback (most recent call last):
002 |   File "<string>", line 6, in <module>
003 | IndexError: list index out of range
mint jewel
#

you may want to look into parametric tests

#

such as hypothesis

old rover
#

what if you had a lawn of grass that's 100 by 100 and you had to mow it all

#

what's the runtime???

#

that sounds like a FAANG question to me

raw zealot
#

Wut

#

Thats not enough data

old rover
#

the question?

raw zealot
#

Yes

mint jewel
#

you can determine complexity on things that have some number or numbers which describe its input

old rover
#

oh it's a square

#

does that help

mint jewel
#

you didn't say what is the number that changes here

old rover
#

check it out if you have time

#

the number that changes???

agile sundial
#

big O is an expression that describes how the runtime changes when you change the input size

raw zealot
#

The time for mowing a single grass should be given

#

👀

old rover
#

well it's cracking the coding interview

#

she can't even explain big O notation without assuming you already know stuff

#

if you can't explain topics to me like a 5 year old i'm not reading the book sorry

raw zealot
#

HMM

old rover
#

the 5 year old strategy is from TAs at my college

mint jewel
#

lets actually state a good question

what is time complexity of mowing a square lawn with a side of N
old rover
#

she said it's O(a) and a is the area of grass

mint jewel
#

think about how does the amount of work scale in relation to N

#

that is correct

old rover
#

or it's O(s^2) which is the length on one side

mint jewel
#

that is also correct

old rover
#

but how did she even figure that out

#

well it's square so

mint jewel
#

well, how much longer does it take to mow 100 Ares than 50 Ares

old rover
#

100*100 v 50 *50?

mint jewel
#

an Are is a unit of area

agile sundial
old rover
raw zealot
old rover
#

i know all the stuff in automate the boring stuff

#

but i can't do hackerrank problems

mint jewel
#

Are is also correct

uneven jungle
#

@old rover then you only have the fun left, good for you

mint jewel
#

acre is not a metric unit

#

Are is 100 sq. m.

#

how is this unit so obscure

old rover
mint jewel
#

alright, new question

#

how much longer does it take to mow 1000 square meters than 500 square meters

old rover
#

i can't upload images here right?

mint jewel
#

you can

old rover
#

ok hang on

mint jewel
#

at least I think so

raw zealot
#

👀

old rover
#

this is in C++? or C? or C#? idk

#

but why is it O(a*b) instead of O(N^2)

#

bc of the inputs???

mint jewel
#

well, what would be N in O(N^2)

raw zealot
#

I guess cpp

mint jewel
#

that is pseudocode probably

#

cpp doesn't have for in

old rover
#

yeah i think it's psuedocode

#

it probably is

raw zealot
#

Yee, I have seen it , too many curly brackets {}{}} in a pseudocode

#

Like Google interviews

#

Lol 👀

old rover
#

google interviews got every damn company thinking they're google

#

anyways i still don't get why it's O(a*b)

mint jewel
#

well, what do you think it is?

old rover
#

i don't think it's O(N^2) bc it's not a nested for loop

raw zealot
#

🤓 aight , see u guys after understanding data structures

old rover
#

peace out dude

raw zealot
#

Bai

agile sundial
#

there is a for loop inside of a for loop

old rover
#

ok then i don't get it at all

raw zealot
#

Double loop

#

O(a*b) 🤔

old rover
#

O(a*b)

raw zealot
#

Looking like a func

#

Lol

mint jewel
#

well, you have 2 inputs

#

both of which affect the time

#

so the O obviously has to respect both

old rover
#

uhhhhh

#

ok

raw zealot
#

What actually

#

Is

#

O

old rover
#

good question

mint jewel
#

big O notation is a way to describe how expensive something is for really large inputs

old rover
# raw zealot O

In academia, big O describes an upper bound on the time. An algorithm that prints all the values in an array could be described as O(N), but it could also be described as O(N2), O(N3), or 0(2N) (or many other big O times). The algorithm is at least as fast as each of these; therefore they are upper bounds on the runtime. This is similar to a less-than-or-equal-to relationship. If Bob is X years old (I'll assume no one lives past age 130), then you could say X .$. 130. It would also be correct to say that X .$. 1, 000 or X .$. 1,000,000. It's technically true (although not terribly useful). Likewise, a simple algorithm to print the values in an array is O(N) as well as O(N3 ) or any runtime bigger than O(N).

mint jewel
#

its not as useful as people try to make you believe

old rover
#

there you go that's straight from cracking the coding interierw

raw zealot
#

Clipboard huh

old rover
#

i am gonna use that giant wall of text every time someone asks

#

kidding

old rover