DEV Community

dev.to staff
dev.to staff

Posted on

5

Daily Challenge #281 - Area or Perimeter

You are given the length and width of a 4-sided polygon. The polygon can either be a rectangle or a square.

If it is a square, return its area. If it is a rectangle, return its perimeter.

area_or_perimeter(6, 10) --> 32
area_or_perimeter(4, 4) --> 16

Tests

area_or_perimeter(5, 5)
area_or_perimeter(10, 20)

Good luck!


This challenge comes from no one on CodeWars. Thank you to CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!

Want to propose a challenge idea for a future post? Email yo+challenge@dev.to with your suggestions!

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

Top comments (9)

Collapse
 
qm3ster profile image
Mihail Malo β€’
        mov     rcx, rsi
        imul    rcx, rdi
        lea     rax, [rsi + rdi]
        add     rax, rax
        cmp     rdi, rsi
        cmove   rax, rcx
        ret

ooga booga

Collapse
 
codeperfectplus profile image
Deepak Raj β€’
def area_or_perimeter(a, b):
    if a == b:
        return a * b
    else:
        return 2 * (a + b)

assert area_or_perimeter(5, 5) == 25
assert area_or_perimeter(10, 20) == 60
Collapse
 
rafaacioly profile image
Rafael Acioly β€’
return a * b if a == b else 2 * (a + b)

:)

Collapse
 
peter279k profile image
peter279k β€’

Here is the simple solution with Python:

def area_or_perimeter(l , w):
    if l == w:
        return l * w
    return (l + w) * 2
Collapse
 
cromatikap profile image
cromatikap β€’ β€’ Edited

In js: let area_or_perimeter = (x, y) => x === y ? x*y : 2*(x+y);

Pass all the tests.

Collapse
 
vyasriday profile image
Hridayesh Sharma β€’ β€’ Edited
const area_or_perimeter = (a,b) => a===b ? a*b : 2*(a+b);

Enter fullscreen mode Exit fullscreen mode
Collapse
 
gagandureja profile image
Gagan β€’
def area_or_perimeter(l,w):
    return l*w if l==w else 2*(l+w)
Collapse
 
alxgrk profile image
Alexander Girke β€’

In Kotlin it would be:

fun area_or_perimeter(a: Int, b: Int) =
        if (a == b)
            a * b
        else
            2 * (a + b)
Collapse
 
hasobi profile image
Hasobi β€’

It's quite simple, check whether a=b if it's true return a*b if it's not, return (a+b)*2.

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

Rather than just generating snippets, our agents understand your entire project context, can make decisions, use tools, and carry out tasks autonomously.

Read full post

πŸ‘‹ Kindness is contagious

Please leave a ❀️ or a friendly comment on this post if you found it helpful!

Okay