samedi 3 septembre 2022

How to restructure nested if else statement

Say you have the following table for a monster object:

Gender Age Type Result
Male Young Fire 1
Male Old Ice 2
Male Old Wood 6
Female Young Fire 4
Female Old Ice 5
Female Old Wood 8
Other Young Fire 7
Other Old Ice 10
Other Old Wood 20

If I were to have a monster and input it to get a result; how would I get the result of the monster object without making a long nested if-else statement?

if(monster.Gender == Male && monster.Age == Old)
    if(monster.Type == Ice)
        return 2
    if(monster.Type == Wood)
        return 6
if(monster.Gender == Male && monster.Age == Young)
etc...

I can't seem to wrap my head around making it extensable and such: say I want to add a column in the future or want to validate something before another thing.

Are there any simple design patterns or the like that I can implement to simplify it and make it extensible?

vendredi 2 septembre 2022

what is the problem in my star pyramid pattern?

i am creating a star pyramid pattern in c, everything is fine but the star "*" is increasing by 2 where it should be increase by 1. here is my code:

#include<stdio.h>
int main()
{
int i,j,k,n;

printf("Enter the number of rows: ");
scanf("%d", &n);

for(i=1; i<=n; i++)
{
    for(j=1; j<=n-i; j++)
    printf(" ");
    for(k=1; k<=(2*i-1); k++) // here k should increase by 1 but it is increasing by 2
    printf("*");
    printf("\n");
}
return 0;

}

Output is:

Enter the number of rows: 5
    *
   ***
  *****
 *******
*********

as you can see that star is increasing by 2. i don't know why.

What tools/pattern are useful to create an interface that can be made dynamically reusable with generics?

I have an object like this, and I plan to keep this as a template that takes an arbitrary number of fields. I want to design an interface such that i can supply an array of field names and that it can be slotted into this object as suggested below. but im not sure if im doing it correctly, or over complicating it.

   let formResponse: FormResponsePayload<["email", "username"]> = {
      status: 'ERROR',
      fieldsWithError: ['email', 'username'], //empty or filled with the fields corresponding to keys of fields
      fields: {
        email: {
          field_name: 'email',
          status: 'error',
          message: ''
        },
        username: {
          field_name: 'email',
          status: 'error',
          message: ''
        }
      }
    }

the answer I've come up with

//i was looking for something that could unpack my generic of string[]. i found Unpacked on another Stack answer. not totally sure how it works.
type Unpacked<T> = T extends (infer U)[] ? U : T;


interface CheckField<A extends string> {
    field_name: A,
    status: string, 
    message: string
}

export interface FormResponsePayload<A extends string[]> {
    status: 'ERROR' | 'SUCCESS'
    fieldsWithError: Unpacked<A>[],
    fields: {
        [key in Unpacked<A>]: CheckField<Unpacked<A>>
    }
}

jeudi 1 septembre 2022

Nested Loop Pattern in Python

Help How can i make this pattern nested loop?I have tried all I coudnt execute it help Pattern nested loop

Serverless and Observer pattern ( ¿It is possible to listen to events from a lambda? )

I was thinking about serverless architecture and the observer pattern (i think that it does not fit into serverless).

How can I implement the Observer design pattern usings a lambda or similar things (severless architecture)?.

My use case is:

  1. Listen to events from ethereum blockchain usings ethers.js or web3.js. I am doing this from an aws lambda function. The lambda is activated on a scheduled time base event.

  2. Sending some of the recollected data in the previous step to an aws RabbitMQ

¿In this kind of technological stack (lambdas, managed services and so) It is mandatory to listen to from a 100% live application (always on)?

I can't imagine a lambda function implementing this pattern, but...ideas are welcome!

Thanks in advance

Is name of the function too long? [closed]

I'm working on a delivery application that works with delivering parcels to locations, etc.

I'm trying to write the code in the way so it's as clean as possible and it's understandable from naming what is the purpose of variables, functions, etc.

I have this one function though that I just don't know how to name it in the way so it's clear what does it do and at the same time the name is not too long and it's not hard to read.

What the function does is that it get parcels that should have been delivered to some location but parcels were delivered elsewhere.

Here's the name of the function: getParcelsInDepotThatShouldHaveBeenDeliveredElsewhere.

Do you think it's too long and if so, how do you think I can improve the naming, because I have no idea how else should I name it.

how can I make this kind of Container in Flutter

[enter image description here][1]

Who can I make a container like the mentioned below [1]: https://i.stack.imgur.com/DmXQ8.png

If anybody knows who to do it then do let me know