Who would who would play me in the movie of my life?
ConorShia LaBeouf.
BryceConnor had no hesitation. Connor just knew.
ConorWelcome to ADSP the podcast episode 55, recorded on December 5th, 2021. My name is Connor, and today with my co-host Bryce, we live code a solution to a leak code problem in C. This is part one of a two-part episode where in next week's episode we'll solve the same problem but a BQN.
BryceNice shorts, by the way. Those are nice shorts. I'm admiring your shorts.
ConorI'm wearing shorts?
BryceYeah, I was admiring your shorts. I like them. They're a good color.
ConorYeah, Lululemon baby.
BryceYeah. Although it's winter. Why are you wearing shorts?
ConorOh, it's toasty in my apartment. I haven't even turned the heater on.
BryceWhat is toasty? What is to and and and if you give it to me in Celsius, I'm not gonna be amused.
ConorUh I don't know. I don't know how warm it is in my apartment. I have no idea. But not cold enough to require me uh wearing pants. Are we recording? Is this are we good? Are we good to go? Of course I'm recording. Yes, here we go, Bryce. Are you ready? I also yeah, are you ready? In celebration. So I got too thirsty because Bryce was like 17 minutes left uh late.
BryceI was savoring a cupcake. I sent you a polite message informing you that I was savoring a cupcake.
ConorThat's true. I'm not sure how that took 17 minutes, but I had to open my grapefruit bubbly that I was waiting. But because we're celebrating, because we're celebrating Bryce, I got another bubbly, and this one's Lyme. Here we go, folks. Here we go. Episode, what episode is this? Is episode like 56? Woo! Right there, baby. Right there.
BryceAlright, so so so a couple couple things. One, clearly, you've never savored a cupcake before. Um I'll have to change that. Uh two, what are we celebrating?
ConorOh, yeah. So remember two episodes ago, I said, oh, I'm not gonna get to update the listener on how my 10k race goes because we're gonna be talking to the Neblers. But I didn't realize that was next weekend, so we're recording today, and I did a 10K in Toronto yesterday and a 10K in Vancouver seven days ago, and I smashed my personal record by two minutes. Previously it was 3723, and it is now 3529.
BryceWow, that is a pretty good 10k time, folks. Let me tell you.
ConorIt's uh yep, it's good. And uh I was beat by a 14-year-old. That doesn't feel good. Um congrats to Steph. Uh I gotta admit, I was running alongside him in second place, like tied for second, from like kilometer two and a half to five, and I thought this kid's running way too fast, he's gonna gas out, and then at the five kilometer mark, I'm the one that is starting to gas out, and then he just like runs away, and I'm like, I gotta reassess where I'm at. Someone told me later I should have got his autograph because he's probably gonna end up in like the Olympics or something later on, which is a good point.
BryceSo wait, did you did you finish second in this 10K?
ConorThat 10K uh that was in Vancouver, I finished third. Uh because yeah, the the 14-year-old beat me. Yep.
BryceUm I have not I haven't ridden my bike. I mean, I'm I spin in the gym, but I haven't ridden my bike since October because it's cold. But good news, my uh something like happened when I came back from Florida. My like cold uh adaptivity kicked in. And like I've been like walking around in like you know, a t-shirt the past few days, and I've been like, you know what? I I now I remember what it was like to be cold in New England as a kid, and I kind of like it. It's not that bad. So something just something just kicked in.
ConorNo to the cold. We don't like the cold.
BryceNo, I I I do like the cold. I do like the cold. I just it just took me a little while to get used to uh uh the cold and uh and remember that I did like it. Oh, I got my uh hang on. Let's see. I got my COVID booster today.
ConorOoh, how you feeling?
BryceUh I'm feeling fine thus far. Uh my my arm's a little bit sore. I imagine I'm gonna feel not not as good tomorrow. Yeah. But what about you? Have you got you you gotten your booster yet? Are they giving out boosters in Canada?
ConorUh I know that uh my grandmother got her booster in Vancouver months ago, at least. Um, so I know that they are giving them out, but definitely I don't think um it's you know white dudes in their 30s are allowed to get them yet. Um at least if at least I haven't heard anything about it. Um and I would have heard because I've been first through the door every opportunity they've when they've opened it up for me to get it.
BryceSo well, you should m move on down to move on down to America and you can get yourself a COVID booster.
ConorUm yeah, we'll think about it. I mean, I've got a race in Austin, Texas.
BryceUm you're going to Austin, Texas?
ConorI know, so that's the thing. I'm not sure I'm going because of Omicron. Um and uh it's on January 23rd. We're recording this today. Is what the December 5th? Um so depending on how things unfold.
BryceConnor, it's like 2022. Like, has that hit you yet?
ConorWhat does that mean?
BryceLike, it's like 2022. Like, that's the year that it basically is.
ConorI mean, that is that is one plus 2021.
BryceYou're not, you're not, you're not understanding, you're not understanding what I'm saying.
ConorBreak it down for me, Bryce. Break it down for me.
BryceIn my mind, last year was like 2011.
Conor2011?
BryceYeah. Like, where did like the past 10 years go? We're like old men now.
Conor2011 feels like doesn't feel like yesterday, it feels like three decades ago for me.
BryceYeah, it does it. It is starting to feel that way.
ConorLike in 2011, I guess it depends on what month you want to choose, but like I was in like halfway through university, I had moved to Toronto for the first time and was working for an insurance company called John Hancock, which is a subsidi at the time it was a subsidiary of Manulife. I mean, that's what I did for eight months of that year.
BryceIn 2011, that that was my that was my second, that was my first year at LSU. So I had just come back from my first time at the supercomputing conference, which was in Seattle. Um, and that was like the third tech conference I went to. I'd already been to the I fun fact, most people think that the first conference that I went to is BoozCon 2011 because I talk about how it was such a life-changing experience for me. But actually, I went to Linux, the Linux Foundation Collaboration Summit, um, or some some it was something with that sort of title um in San Francisco, like a month before that, to talk about compiling the Linux kernel with uh Clang and LLVM. Um and I met I met Marshall Clow at the Linux uh foundation summit thing, and then I went to BoostCon 2011, and I also saw Marshall Clow, and they were like two very different communities. So I was just like, oh, this guy just goes to every tech conference.
ConorWhat was the first conference that you spoke at?
BryceThat that Linux um uh foundation that God, I I won't remember what the name is, but that Linux uh foundation collaboration summit was the first conference I spoke to.
ConorIn 2011?
BryceYeah, yeah, it was the first conference I attended, the first one I spoke at because they wanted me to talk about um uh LLVM, compiling LLVM, compiling Linux with LLVM. Um that's that's crazy. Hang on, but you spoke at the first conference that you went to.
ConorYeah, but that's what I'm saying is that like so 10 years ago for you, you were sort of already in the C community. I was in a whole different uh education path/slash career path. I didn't speak or go to a tech conference until 2019, which was two years ago.
BryceI know, I remember I was there poking you with a stick, making you speak.
ConorIt's just crazy to think that of the last decade, like 80% of it wasn't doing any of this stuff, but like you skip back a decade and you were you were starting that like it's it's uh we do share a lot, you know. We've mentioned this a couple times about our first languages, TI Basic and Quasi dropping out. I mean, you actually did drop out or kick got kicked out, and I kind of got kicked. Anyways, there's a lot of similarities, but when you look, when you compare the last decade and where we were at, that's totally different. Um I was not expecting you to say oh. I like for some reason I thought you were gonna say, Oh, my first time I spoke was like at you know, CPPCon 2015 or 16 or something, because I feel like that's the first YouTube talk that I've seen. No, like that goes that far.
BryceThere is somewhere a recording of the first talk that I gave at BoozCon, not the first, not the talk I gave at the the Linux Foundation thing. Um there is a recording of that talk that I gave at BoozCon somewhere, uh, but like very well hidden because it was bad. I stayed up, I I was up until 4 a.m. the night before. It was an 8 a.m. talk because I was still making slides, and I'm I recall I made a bunch of really bad Star Wars references. Yeah, that was a YouTube?
ConorIs this on YouTube?
BryceIt might be on YouTube. All right.
ConorAnd what year is this? What year is this?
BryceBoostCon 2011. I was talking about um uh Boost Spirit U Tree, um uh which was uh an AST data type for um Boost Spirit, which is a parser framework, which is the first not not the first thing I worked on in Boost, but uh a project that I rapidly gravitated towards. Um because when I my first exposure to Boost was I was using it for this, you know, as I we've discussed before, I you know started learning to program to write this game. Um and then I got you know, I learned about Boost because I was using it for this game project, and um I very quickly got sidetracked working on you know Boost. Um the thing that I for my first exposure to Boost was Boost String Algorithms Split. Um I don't remember why I needed it, but it was a text-based game that I was working on. Uh they're called MUDs, multi-user dungeons or dimensions. And uh I think I was using BoostString Algos Split to do some tokenization of input lines. Um and there was some bug in it, and I like reported the bug and like sent a patch to fix the bug. Um, but then I think from there somebody must have told me to use Boost Spirit instead for my parsing needs, and like that's sort of how I I got from submitting this bug about um boost format or boost boost string algorithm split to um uh working on Boost Spirit. Um and then that's that's how I met Hartmett Kaiser, because he, you know, while he's known these days as being the guy who uh runs the HPX project, the parallel runtime, um he was also one of the maintainers and founders of Boost Spirit. And so that's how we initially connected was that I was contributing to Boost Spirit, and then that's you know, I randomly asked him for a job on IRC having never met him, and that's when he said you should fly on down to Louisiana. Yeah.
ConorDamn, ma'am, you got started so early.
BryceI it was it was really it was really weird. I um I like started learning C ⁇ , and then three months later I was like I had like commit access to Boost. And then three months after that, I was um, you know, I was the the the release um manager for one of the Boost Spirit releases, and then like three months after that I was speaking at BoostCon, and then three months after that I was at LSU, and then three months after that Hartman and I were starting our own research group, and I had like more responsibilities than than I uh I I should have had as a 19 or 20-year-old college dropout. It was a very it was a very crazy period of my life.
ConorThey should make a movie. I'd watch it.
BryceI I I I would watch it too. Who would who would play me in the movie of my life?
ConorShia LaBeouf.
BryceI like how Connor had no hesitation. Connor just knew.
ConorI just it's it's funny too, because like I did not have that pre-prepared, but like it's it's uh if we're playing like the young version though, unfortunately he's a bit too old now.
BryceBut like No, no, no, but like young Shia LaBeouf would be perfect too.
ConorYeah, would he he would absolutely be perfect? Um I don't know why that came to mind so quickly. I don't know. A young Tom Cruise?
BryceI was I was thinking Brad Pitt. I was thinking Brad Pitt. Brad Pitt? Yeah.
ConorI'm not I'm not that's generous of you, Bryce.
BryceUh you are a very good looking, a very good looking individual. I mean, that's uh I mean I mean you said that's very generous of you, but you but you led with Tom Cruise.
ConorSo uh that's only because there was a girl in university that called me Tom because she said I looked like Tom Cruise.
BryceOh speaking speaking of girls, speaking of girls. Alright, well we should probably we should probably uh get to the podcast.
ConorWe can we can uh do something real brief to round out our talking about whatever we were talking about um by doing another real brief leak code and BQN if you want. Um it's a it's a very short solution, and I was just I was so in love with it. Um I've just been I've been on a a tear lately. But let me share screens first. And actually, maybe we'll do it, we'll see how quickly we can do this. What is it? 8 637. We probably chatted for 15 minutes. Let's see if we can solve this in both C. I'll type, you tell me what to type in Godbolt. Oh yeah. So here's the here's the problem.
BryceYeah.
ConorFind or do you want to read it? I'll read it. Uh find target indices after sorting array. You're given a zero index integer, array nums, and a target element target. A target index is an index.
BryceOkay.
ConorA target index is an index i such that uh the index of your array nums is equal to the target. And the question asks you to return a list of the target indices of your array nums after sorting nums in non-decreasing order. Um and if there's no target indices, just return an empty list. So for example, I'm so confused. If your list is one, two, five, two, three, and your target is two, after sorting your uh array nums, it would be one, two, two, three, five, and then you just need to return all the indices that are equal to your target value. So because two is your target value, that corresponds to index one and index two.
BryceAlright, I understand. You're only ever given one target?
ConorCorrect, one target. Um and so the second example is the same list, one, two, five, two, three. Your target now is three. And if you sort this, once again, it's one, two, two, three, five, and the three is in index three, so that's your answer. The third example is uh target equal to five, same list. If you sort it, five is gonna be the last number in the list, which will be equal to index four. And then if your target is four, that value doesn't show up in one, two, five, two, three. So you end up with an empty list. Let's head over to, so let's copy. Let's copy. Or actually, we can do it straight in here. What are we talking about? What are we talking about? Um let's zoom in and change this to auto. Um, so we've got target indices as a function. It takes a vector.
BryceWhy is there a class here? I'm I'm so confused.
ConorUm that's just the way that it works for lead code.
BryceThat's it should be a function. And actually who do I who do I call about that?
ConorThe lead code folks. Um Java nonsense. How do you want to solve this, man? What do you want to do?
BryceWell, so I mean, my first thought is that we want to sort the array, but I was actually I was actually wondering whether we could um I wonder if they have that.
ConorI wonder if they have um C ⁇ 20. What do you think?
BryceSo I I mean I mean obviously we have to sort the array, but I I'm just wondering whether we can whether we can somehow we have to go through we have to perform the work that would be involved in sorting the array, but I wonder whether whether um calling std sort is actually the the best way to do this. Connor tried calling std ranges sort um and discovered that leak code.
ConorYeah, I think they only have C plus plus 17.
BryceUm have C20, which is not shocking to me given that the like leak code structure for a problem is like very clearly like 90s um object-oriented C. Uh no offense to anybody that works at lead code, but a little bit of offense was intended.
ConorSo we've sorted our array. What do we want to do next? Go back up to the problem. Actually, you know what? We definitely we're switching, we're switching to C we're switching to gobble, because I want C20. I ain't messing around.
BryceSo you want to find I mean, call std find.
ConorWhy do we need stdfind?
BryceWe'd we'd call std find because we need to figure out where the uh we need we need to find the first element in the sorted list that matches um the target. And what we really need is like an equal range. Something like that. An algorithm.
ConorIt is an algorithm.
BryceThere we go. CI know stuff.
ConorUh I guess we need to do include range.
BryceAlthough Yeah, for forget find. We should just be calling equal range, right? Which is not actually uh a C20 thing. Equal range was an algorithm uh pre-C plus plus 20. Um before there was, you know, a notion of ranges in the language. Or a no a no the notion of ranges in the language that we currently have. Equal range, you give it um you give it uh uh a pair of iterators and a value, and it returns you a pair of iterators, which is um all the elements uh that are equal to the value, and it expects that the input sequence is partially ordered with respect to the value, um, which is gonna be the case here because we've we've done this sort. Wait, okay, hang on, hang on, hang on. Um we only we only need to sort until I'm not convinced that we need to sort the entire array, right? Because we only need to like sort up until we've gotten to gotten to the the the the target that we're looking for. And I'm wondering if there's some clever sorting algorithm that lets us only sort.
ConorUm so what is this gonna return? This is gonna return us probably a pair?
BryceA pair, yeah, a pair. What are you doing, D? Aren't you gonna be destructuring that?
ConorThey're gonna be iterators, but yeah, we probably can do that. So let's do first.
BryceOkay, so so we have the iterators and it wants us to return the indices.
ConorYeah.
BryceUm well then uh we're gonna take those iterators, those are gonna be random access iterators, so we can just um we can just do some some pointer arithmetic.
ConorUm yeah, we have currently we have std colon ranges, colon colon sort, and then we've got structured bindings, uh f comma l equals std colon ranges, colon colon equal range, and if we dereference both F and L, it gives us the values two and three. And so now we just need to get the indices which correspond to basically what?
BryceWait, why is it given it's giving us two? Yeah, so so so what we need to do is we need to um uh just do some uh some Iterator arithmetic here. Yeah, yeah, sure. You can call distance. Sure. Oh, you just want to figure out like how many there are. Sure, fine. That's okay. Oh, actually we can we can um we can use iota here.
ConorThat was what I was thinking.
BryceYeah, because if if the answer is more than one element, um then it's gonna be they're gonna be consecutive. Um so we just need to figure out um we just need to figure out uh the starting value for iota, which is gonna be um the distance from uh the beginning of uh the input to that first element that we found. Um and the we want I we we'll want iota for um the the the size of the output is gonna be because we'll we'll just we'll just yeah yeah you I see what you're doing there. So you're you're creating a std vector with default initialized ints. Um and then uh uh and you figure out how many there are by taking the distance of from the the uh start of the equal range to the end of the equal range. Um and then it that start should be S there. Um I'm not doing a great job of explaining this for the listener. Um but uh but yeah, okay. So I mean there's there's two things that you gotta figure out here, um, which is how many um how many indices are you returning, and you do that by computing the distance between the uh the uh iterator to the start of the equal range and the iterator to the end of the equal range. And then you need to figure out what is the first indices index that you're returning, um, and you figure that out by computing the distance from the first iterator in the input to the first iterator that that matched the first iterator of the equal range. Um and then you create a vector of n elements, which is however many elements uh uh you've determined are in this equal range. Um and then you do a std iota on that vector, and the initial value for the iota is the distance from uh that starting distance that you computed, which was the distance from uh the beginning uh iterator to the input to the first iterator from the equal range. Yeah, that made sense to me. I'm not sure that anybody that's listening is gonna follow anything that I just I think it was pretty clear.
ConorYou know, you sort, you stood range is sort, then you stood range is equal range. Then you yeah, you have to do a little sort of figuring out what is the index of the start of your equal range, and then you need to create a vector and then use iota to fill the indices using that sort of start.
BryceUm But but do you do you really need to sort the entire thing?
ConorYou don't need to sort it at all technically, but I mean this is the direction I was hoping you'd go because it's it's I mean, if you look at the input.
BryceIn my defense, the first thing I said is, well, my nit my my intuition is to sort it, but I'm wondering if we don't have to do that. So when you tell me later that I don't need to actually sort this thing, I just want it on the record that my first gut reaction was, I don't think we have to sort this.
ConorYeah.
BryceThe reason that I don't think that we need to sort this and that it's inefficient to sort this is um you don't care about the order of the things after the the target index.
ConorYou don't even really care about the order before either.
BryceRight. That's that also is true. Right, right, right, right, right. All you care about all you care about is how many are in each of those.
ConorExactly, exactly. So you can technically do this in l in linear time.
BryceUm but do we use partition here?
ConorI mean, if I were gonna solve it in the linear time method, I'd just do two count ifs. Count if less, count if equal. Then you've got your values that you need in order to use iota.
BryceWalk me through that.
ConorSo say so here your target value is two, count if less than two is gonna give you one. And then count if equal. What?
BryceBastard. That's very clever.
ConorAnd then count if equal gives you your basically so you so here in our solution that we're looking at, we have n, which is the number of equal elements, and then start, which is the distance between the equal range, beginning of the equal range, result of equal range, and the start of the array. You can get those two values by just doing count ifs with uh lambdas, one that checks less than target value, one that checks equal to. However, I'm glad you went with the sort because we're gonna hop over to BQN now. And we're looking at one, two, three, four, five, six, seven lines. But you know, you could code golf this if we had access to like thrusts um uh counting iterator. We could just we could reduce the last three lines into a single line with a range constructor and sort of creating this in place in a constructor instead of having a constant iota.
BryceWhy aren't we returning an iota view here?
ConorUm because the question asks you to turn a vector and those are not implicitly.
BryceThe question is ill form. You can't do anything with views.
ConorThat's true. We can we can uh let's see what happens if we go, we delete these last three lines and we go return uh stud. Is it views or view? View um iota, and we're gonna go with start and start plus n. And you said it was views, but um actually maybe that's because I'm not including the range header. Compiling, compiling, and it looks like that is correct. So yeah, we've we've reduced it if we loosen the restrictions of this to one, two, three, four, five lines of code. First one sort, second one is ranges equal range. Um then there's two std distances to get n and start, and then we're returning a view Ziota using start and start plus n. So not too bad. Five lines, pretty, pretty nice in my opinion. Um, sure there's more performant code uh doing the linear stuff, but I like this solution quite a bit. And now let's do go and do something similar in BQM. So let's hop over to our next gen APL. Stay tuned for part two next week. Thanks for listening. We hope you enjoyed and have a great day.