Journal Articles

CVu Journal Vol 32, #3 - July 2020
Browse in : All > Journals > CVu > 323 (11)

Note: when you create a new publication type, the articles module will automatically use the templates user-display-[publicationtype].xt and user-summary-[publicationtype].xt. If those templates do not exist when you try to preview or display a new article, you'll get this warning :-) Please place your own templates in themes/yourtheme/modules/articles . The templates will get the extension .xt there.

Title: ACCU York – May 2020

Author: Bob Schmidt

Date: 06 July 2020 17:01:47 +01:00 or Mon, 06 July 2020 17:01:47 +01:00

Summary: Frances Buontempo reports from a local ACCU group.

Body: 

ACCU York held a virtual workshop on May 28th. Gareth LLoyd led the session. We were given a coding challenge, which has been used has an interview question:

Given a string, like "Can you reverse these words?", write code to reverse the words in the string to get "words? these reverse you Can" without extra memory allocation.

That caused a degree of discussion, and may have been off-putting for people more used to other languages. With hindsight, I wonder if we could have first reversed the words in a string by any means, then maybe if people had used a vector to copy the words into, then upped our game to avoid the extra copies.

We discussed possible approaches and somebody waved a copy of K&R around (https://en.wikipedia.org/wiki/The_C_Programming_Language: The C Programming Language by Brian Kernighan and Dennis Ritchie). I love this book, but it set me thinking about C-style pointers to solve problems.

We were encouraged to try out the problem in Godbolt: https://godbolt.org/z/HpVcXw This contained a main and a function to fill in.

#include <string>
#include <cassert>
void reverseWords(std::string &) noexcept
{
  // code here
}
int main()
{
  using namespace std::string_literals;
  auto str = "Can you reverse these words?"s;
  reverseWords(str); 
    // Use no extra allocated memory
  assert(str == "words? these reverse you Can");
}
			
Listing 1

Had I followed instructions and done what I was told, passing standard strings around may have stopped me making a mess on paper with a pen, attempting to come out with neat K&R style char *s and char * d algorithms. Furthermore, the assert would have a provided a way to check what I was doing. Oh well.

Most of us came out with something similar – using an index which we had to increment, or a pointer, to walk the string. Without spoiling the workshop for people who haven’t done this yet, the point emerged that C++ has many algorithms that make your life much easier.

We had a long discussion afterwards. In particular, using the ‘s’ suffix, from the string_literals namespace led to a detailed talk about argument dependent lookup – ADL. Again, this may have put off some newer C++ programmers. However, the flow of the examples and chance to talk was very well balanced. If you’ve not had the chance to attend ACCU York yet, the next meetup will probably be online too – join in. They’re a friendly bunch.

Frances Buontempo Frances has a BA in Maths + Philosophy, an MSc in Pure Maths and a PhD technically in Chemical Engineering, but mainly programming and learning about AI and data mining. She learnt to program by reading the manual for her Dad’s BBC model B machine.

Notes: 

More fields may be available via dynamicdata ..