ALLAN AVELARTechnology Solutions
🍰Good Afternoon,<username class="jsx-1036735364">Guest</username>!Welcome to my website!Progressive Web App (PWA)Install in your deviceChromecast Controlsstatus: not playing or showingVolumeDown50%VolumeUpPlayPausestoppedStopResetFullscreenExpandRestricted accessLogin / RegisterEnter with your credentialsRestrict Access ManagerRequest your Access CodeAccount DetailsYour personal and access dataProjects / ServicesYour contract historyProducts / DownloadsYour purchase historyChromecast AppsYour streaming contractsCredits / PurchasesYour balance and credit historyAccess ManagerYour restricted content accessNotificationsNotifications enabled for youDisconnectDisconnect your userAvailable contentHomeAbout Me6Personal ResumeA little bit of how it all startedProfessional ResumeA little about my careerWork MethodologyHow I make it happenPersonal MemoriesRestricted ContentCurriculum VitaeRestricted ContentTrips & PhotosRestricted ContentAreas of Expertise10All my servicesSummary of all areasCTO as a ServiceGet a part-time basis C-LevelCorporate ArchitectureAgility and technology as a philosophySoftware & TechnologyComing soonFrontend DevelopmentComing soonBackend DevelopmentComing soonStrategy & ConsultingComing soonPlatform & DevSecOpsComing soonCustomer ExperienceComing soonAgile CultureComing soonInnovation Hub6Innovation CultureAdvantages and how to implementDistributed LT & BlockchainComing soonData Science & AnalyticsComing soonCybersecurity PlatformComing soonIntelligent AutomationComing soonAI & Machine LearningComing soonCourses & Training9Distributed LT & BlockchainComing soonCybersecurity PlatformComing soonIntelligent AutomationComing soonFrontend DevelopmentComing soonBackend DevelopmentComing soonPlatform & DevSecOpsComing soonAI & Machine LearningComing soonProgressive Web AppsComing soonAgile CultureComing soonContact MeDEVELOPED PROJECTSAll my projectsSummary of all areasFinancial Institutions3Santander Totta BankRestricted ContentSantander Brazil BankRestricted ContentSafra Bank S.ARestricted ContentWeb3 / Blockchain15Bitcoin (BTC)Restricted ContentEthereum VM (ETH)Restricted ContentRipple (XRP)Restricted ContentHyperledger.orgRestricted ContentInternet ComputerRestricted ContentStarknet (Cairo)Restricted ContentPolkadot (DOT)Restricted ContentDeFi DashboardsRestricted ContentToken MarketplacesRestricted ContentCrypto CheckoutsRestricted ContentCold / Hot WalletsRestricted ContentWallet ConnectorsRestricted ContentNon Fungible Tokens (NFT)Restricted ContentDescentralized Identities (DID)Restricted ContentDescentralized Organizations (DAO)Restricted ContentOld Projects29TIM | Interactive Voice ResponseAdobe Flex, Actionscript 3, Java, SpringACS | Follow EnergyAngularjs, HTML5, CSS3, C#, REST APIACS | Gateway CE50Angularjs, HTML5, CSS3, C, C#, LUASKY | Selinho de NatalActionscript 3, .NET, REST API, JSONIntel | Futuro de PresenteActionscript 3, .NET, REST API, JSONNestlé | Baú de DiversõesAS 3, .NET, REST, Facebook OpengraphMarlboro | Red RacingAdobe Flash, Actionscript 3, JSONHP | Cartucho PirataAdobe Flash, Actionscript 3, REST APIHP | Youtube StoreAdobe Flash, AS3, YouTube Data APICasas Bahia | BahianinhoActionscript 3, .NET, REST API, JSONAdria | TortinhasAdobe Flash, Actionscript 3, PHPUniverso Online | UOL FinanceActionscript 3, REST API, JSONUniverso Online | UOL MaisActionscript 3, REST API, JSONChevrolet | CaptivaActionscript 3, PHP, MYSQLGafisa | Edifício ColaborativoAdobe Flash, Actionscript 3Pão de Açucar | Sommeliers ClubAdobe Flash, Actionscript 3Pão de Açucar | Desafio ColeçõesAdobe Flash, Actionscript 3Antarctica | Circuito OriginalMacromedia Flash, Actionscript 2, XMLNestlé | Nescau 2.0Macromedia Flash, Actionscript 2, XMLSchin RefrigerantesActionscript 2, FMS 2, PHP, MYSQLMercedes-Benz | SLK 2005Actionscript 2, XMLMercedes-Benz | Classe CActionscript 2, XMLMercedes-Benz | Classe AActionscript 2, XMLMercedes Benz | SprinterActionscript 2, XMLMercedes Benz | Motores EletrônicosActionscript 2, XMLBanda NX ZeroMacromedia Flash, Actionscript 2Lew´lara TBWAActionscript 2, PHP, MYSQLMaison CriolaActionscript 2, PHP, MYSQLBondage MusicActionscript 2, PHP, MYSQLBlog PostsHackerRank5Sock MerchantWarm-Up ChallengesCounting ValleysWarm-Up ChallengesRepeated StringWarm-Up ChallengesJumping On CloudsWarm-Up ChallengesLeft RotationWarm-Up ChallengesLegal TermsTerms and ConditionsPrivacy PolicyDisclaimer
Username / PasswordBlockchainForgot your password?LoginDon't have an account? Look at a demonstration!
🚀 April 24, 2018 ♻ May 21, 2018reading 8 min reading

Warm-up Challenges ⚡ Jumping On Clouds

I'm posting about my solutions to the HackerRank challenges as a way to improve my learning, reinforce my knowledge, and establish an understanding of the concepts covered. If I help someone by sharing along the way, even better!

Jumping On Clouds is one of the initial challenges with 'EASY' difficulty.


What's the challenge?

Emma is playing a new mobile game that starts with consecutively numbered clouds. Some of the clouds are thunderheads and some are cumulus. She can jump into any cumulus cloud with a number equal to the current number of clouds plus 1 or 2. She must avoid the storm clouds. Determine the minimum number of jumps that it will take Emma to jump from her starting position to the last cloud. It is always possible to win the game.

For each game, Emma will be given an array of clouds numbered 0 if they are safe or 1 if they need to be avoided. For example, c = [0, 1, 0, 0, 0, 1, 0] indexed from 0...6. The number in each cloud is its index in the list, so it would have to avoid clouds with indices 1 and 5. It could take the following two paths: 0 -> 2 -> 4 -> 6 or 0 -> 2 -> 3 -> 4 -> 6 . The first path takes 3 hops while the second takes 4.

What do we have to do?

A function that returns the minimum number of required jumps, as an integer.

Parameters
  • c = an array of binary integers

Sample Input

0 0 1 0 0 1 0

Sample Output

4

Solution

function jumpingOnClouds(c) {

	let steps = 0,
			i = 0;
				
	while (i < c.length - 1) {
		if ((i + 2 < c.length) && c[i + 2] == 0) {
			i += 2;
		} else {
			i++;
		}
		steps++;
	}

	return steps;
}

Logic

let steps = 0,
	i = 0;

First I created two variables; steps to keep count of the minimum number of steps required; i to iterate through the whileloop;

while

I decided on a whileloop instead of a forloop because for the first statement if ((i + 2 < c.length) && c[i + 2] == 0), I want to increment i += 2 and not just by 1.

I always try to jump two clouds. If not possible because of storm cloud, jump 1 cloud. So I do this until the pointer is finally on the last cloud.

Finally, I finalize the function by returning the final count. 🖖


🕵Extra Solutions

Other languages that I speak


💎 Ruby

def jumpingOnClouds(c)
	steps = 0
	i = 0

	while (i < c.length - 1) do
		if ((i + 2 < c.length) && c[i + 2] == 0)
			i += 2
		else
			i += 1
		end
		steps += 1
	end
	steps
end

🐍 PYTHON 2 & 3

def jumpingOnClouds(c):
	steps = 0
	i = 0
	
	while (i < len(c) - 1):
		if ((i + 2 < len(c)) and c[i + 2] == 0):
			i += 2
		else:
			i += 1
		end
		steps += 1
	
	return steps

golang GOLANG

func jumpingOnClouds(c []int32) int32 {
	var steps int32
	i := 0
	for {
		if i + 2 < len(c) && c[i + 2] == 0 {
			i += 2
		} else {
			i++
		}
		steps++

		if i <= len(c) - 1 {
			break
		}
	}
	return steps
}

scala SCALA

def jumpingOnClouds(c: Array[Int]): Int = {
	var steps = 0
	var i = 0
					
	while (i < c.length - 1) {
		if ((i + 2 < c.length) && c(i + 2) == 0) {
			i += 2
		} else {
			i += 1
		}
		steps += 1
	}
	return steps

javaJAVA 7 & 8

static int jumpingOnClouds(int[] c) {
	int steps = 0,
			i = 0;
					
	while (i < c.length - 1) {
		if ((i + 2 < c.length) && c[i + 2] == 0) {
			i += 2;
		} else {
			i += 1;
		}
		steps += 1;
	}
	return steps;

php PHP

function jumpingOnClouds($c) {
	$steps = 0;
	$i = 0;

	while ($i < count($c) - 1) {
		if (($i + 2 < count($c)) && $c[$i + 2] == 0) {
			$i += 2;
		} else {
			$i++;
		}
		$steps++;
	}
	return $steps;

Thought: We can judge the heart of a man by his treatment of animals.
Emmanuel Kant

💡 Latest Insights:

I don't claim that these answers are the best or most efficient, they are simply creations of my mind to solve the problem at that time. Probably today I would do different...
About MeProfessional ResumeWork MethodologyPersonal MemoriesCurriculum VitaePhotos & Travel
AREAS OF EXPERTISECTO as a ServiceCorporate ArchitectureSoftware & TechnologyFrontend DevelopmentBackend DevelopmentStrategy & ConsultingPlatform & DevSecOpsCustomer ExperienceAgile Culture
Innovation HubDistributed LT & BlockchainData Science & AnalyticsCybersecurity PlatformIntelligent AutomationAI & Machine Learning
COURSES & TRAININGBlockchain & Distributed LTCybersecurity PlatformIntelligent AutomationFrontend DevelopmentBackend DevelopmentPlatform & DevSecOpsAI & Machine LearningProgressive Web AppsAgile Culture
Hackathons & Prizes
DEVELOPED PROJECTSWEB3 / BLOCKCHAIN 2017 ~ 2024BitcoinEthereumRippleStarknetDIDInternet ComputerMarketplacesNFTHyperledgerWalletsDAODeFi DashboardsCrypto Checkoutsecurity tokensBANKS 2016 ~ 2021Santander TottaSafra S.A.Santander BrazilOld Projects 2000 ~ 2016SKYIntelACSNinho SoleilHP YouStoreHP Cartucho PirataMarlboro RacingMercedes BenzTortinhas AdriaChevrolet CaptivaGPA Sommeliers ClubUOL FinanceGafisaSprinterCasas BahiaJVCLew´laraPão de AçucarSchin RefrigerantesNescau 2.0PapaizTV CulturaSpacekidsBondage MusicMaison CriolaAntarctica OriginalNX ZeroSLK 2005Classe AClasse CPetrobrasBraskemNíveaJohnson & JohnsonAbyaraTIM (URA)NestléUOL
Blog PostsHACKERRANK 2017 ~ 2018#sockMerchant#countingValleys#repeatedstring#jumpingOnClouds#leftRotation
Contact Me
Terms and ConditionsPrivacy PolicyDisclaimer
This PWA was built with ❤ in my spare time 🤸‍♂️ through of the years...