Code Monkey home page Code Monkey logo

Comments (9)

siebenschlaefer avatar siebenschlaefer commented on July 18, 2024 1

An opaque type is another thing that I wouldn't call "easy". And if you consider dynamic memory allocation (and deallocation), pointers as output parameters, and opaque types as easy then (almost) all exercises on the C track are easy.
I've asked on the forum if somebody has ideas.

BTW: Whether you want to call the type alias table or TABLE is IMHO a minor detail (although I always try to reserve identifiers in "ALL_CAPS" for the preprocessor).

from c.

github-actions avatar github-actions commented on July 18, 2024

Hello. Thanks for opening an issue on Exercism. We are currently in a phase of our journey where we have paused community contributions to allow us to take a breather and redesign our community model. You can learn more in this blog post. As such, all issues and PRs in this repository are being automatically closed.

That doesn't mean we're not interested in your ideas, or that if you're stuck on something we don't want to help. The best place to discuss things is with our community on the Exercism Community Forum. You can use this link to copy this into a new topic there.


Note: If this issue has been pre-approved, please link back to this issue on the forum thread and a maintainer or staff member will reopen it.

from c.

siebenschlaefer avatar siebenschlaefer commented on July 18, 2024

I did play with this exercise a little bit and did not see a way for a straightforward translation.

In the original tests there's some "object" that gets initialized with a number of scores, and there are three four methods:

  • scores() returns all the scores (a array, in the original order), and
  • latest() returns the most recent score (a number),
  • personalBest() returns the highest score.
  • personalTopThree() returns the three highest scores (an array, in non-ascending order).

The "comments" at the beginning of the canonical-data.json say:

This is meant to be an easy exercise to practise:

  • arrays as simple lists
  • instantiating a class

I don't see a way to keep the solution simple and uses arrays and use some kind of class.

If I'd want to keep the exercise simple I would require two three independent functions that take some scores: latest(scores, scores_len) would return the last element of scores, personal_best(scores, scores_len) would return the greatest element of scores, personal_top_three(scores, scores_len, &result) would write the three highest scores to the output variable result, and scores() wouldn't make sense at all because it would just return its argument.

If I'd want personal_top_three() (and maybe scores()) to "return" an array I would either need one or two output-parameters (because I would need to communicate the array and its length to the caller), or I'd have to create a struct that holds the array and its length. And the array might need to be dynamically allocated and later deallocated.

If I'd want to have some class-like structure I'd have to take care of the lifetimes of the arrays, maybe copy or allocate/deallocate them.

Do you have an idea how to keep the C translation close in line with the tests and objectives ("easy", "arrays", "class")?

[edited, i forgot one of the four functions]

from c.

keiravillekode avatar keiravillekode commented on July 18, 2024

To honour the 'class' objective, we can use an opaque data type:

struct table_impl;
typedef struct table_impl* table;

table create_table(int* scores, size_t scores_len);
int latest(table t);
int personal_best(table t);
size_t personal_top_three(table t, int* dest);
void destroy_table(table t);
#include "high_scores.h"

struct table_impl {
};

scores() is omitted as it is not 'easy'.

from c.

keiravillekode avatar keiravillekode commented on July 18, 2024

As most students will be familiar with the use of FILE*, it might be better to use

struct table;
typedef struct table TABLE;

TABLE* create_table( . . . );
#include "high_scores.h"

struct table {
};

from c.

keiravillekode avatar keiravillekode commented on July 18, 2024

We can omit the 'class' objective, as in Awk and Fortran.

int latest(const int* scores, size_t scores_len);
int personal_best(const int* scores, size_t scores_len);
size_t personal_top_three(const int* scores, size_t scores_len, int* dest);

from c.

siebenschlaefer avatar siebenschlaefer commented on July 18, 2024

We can omit the 'class' objective, as in Awk and Fortran.

I'd be OK with that, I think that might be the simplest option.

from c.

ryanplusplus avatar ryanplusplus commented on July 18, 2024

We can omit the 'class' objective, as in Awk and Fortran.

I'd be OK with that, I think that might be the simplest option.

Agreed. Going much further than this approach brings in a lot of complexity.

from c.

keiravillekode avatar keiravillekode commented on July 18, 2024

PR #902 is ready for review. It was automatically closed.

from c.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.