bruceeckel / atomickotlinexamples Goto Github PK
View Code? Open in Web Editor NEWExamples auto-extracted from the book "Atomic Kotlin."
Home Page: http://www.AtomicKotlin.com
License: Other
Examples auto-extracted from the book "Atomic Kotlin."
Home Page: http://www.AtomicKotlin.com
License: Other
I'm condensing multiple notes for the Introduction to Objects section here. None of these are critical issues.
Task Description
getLocation() should return the coordinates as a string (x, y).
Description appears to want ($x, $y)
, but unit tests only pass with ($x,$y)
(space vs no space)
// Exceptions/IntroducingNull.kt, Accompanying text:
But what does it mean that the denominator is zero? Maybe you know how to deal with a zero denominator
// Exceptions/AverageIncome.kt, Accompanying text:
Unfortunately, this doesn't tell us anything about why this error occurred, what the denominator means and whether it can legally be zero in the first place.
Phrasing on both suggests that the word 'denominator' has already been seen in an error message, but that is not the case.
To further emphasize DRY, percentOther
can also use getPercentage()
by passing groceryCart - meats - fruits - vegetables
as the first parameter.
Task Description:
It should choose a hamster by a given name, return it and remove it from the list of hamsters.
Solution code doesn't remove the named hamster. It could include the line hamsters -= hamster
right before return hamster
.
Same logic, put() puts a hamster in the cage, and take() takes one out, so instead of this:
fun takeHamsterByName(name: String): Hamster =
hamsters.getValue(name)
We could have this:
fun takeHamsterByName(name: String): Hamster {
val hamster = hamsters.getValue(name)
hamsters.remove(name)
return hamster
}
Just FYI, IntelliJ Column Selection doesn't work with the IdeaVim plugin, but it does work with regular vim commands for the same feature. This is probably too niche to warrant a mention in the book/course, but I thought I'd mention it here anyway.
Hi! The hint button used to work fine a few days ago and now clicking it doesn't do anything. It's driving me a little nuts, to the point of completely uninstalling IntelliJ IDEA (and any files) and re-installing everything including the course but it still doesn't do anything.
It's just sitting there... begging to be clicked. But once clicked, it does nothing. It just disappoints :(
https://i.imgur.com/C2gfZn5.png
From the book at Number Types
The location where the L appears is unimportant. If one of the values is Long, the resulting expression is Long.
Not true, there is an actual result in case I move L
val to the different place:
fun main() {
val i = Int.MAX_VALUE
println(i + i + 0L) // overflow, because of math rule
}
Anyway, can you explain Type Inference (type rules) algo or point me to an authoritative resource?
i read your book atomic kotlin,
but i have trouble in running "helloworld.kt"
i run helloworld.kt at macbook pro m2 pro, macos ventura 13.3, IntelliJ idea community edition 2023.1
i installed intellij with jetbrain toolbox.
how can i fix this trouble ?
i can't start a page with this book.
please help me
From the Task Description:
For proper alignment use
"%3d".format(number)
to place additional spaces before the number.
With that phrasing I would expect to use the provided expression nearly verbatim. However, exercise code doesn't pass the 2x2 unit test or match the expected 3x3 output in the description without calculating width with (rows * columns).toString().length + 1
and then using "%${width}d".format(number)
, rather than just hard-coding it to 3.
This is apparent after a 'Peek Solution...', but I think a learner would have a better shot at writing passing code on their own if at least one of the following were provided:
For proper alignment use a format string such as
"%3d".format(number)
to place additional spaces before the number.
printSnake(3, 3)
added to main()
to give that use case better visibility, and expected output added to the commented output alongside the expected 4x5 output:fun main(args: Array<String>) {
printSnake(4, 5)
println()
printSnake(3, 3)
}
/* Output:
0 1 2 3 4
9 8 7 6 5
10 11 12 13 14
19 18 17 16 15
0 1 2
5 4 3
6 7 8
*/
If people who are completely new to programming are among the target audience, this might also be a nice place to explicitly mention the pitfalls of hard-coding values. After all, it isn't immediately apparent that output of a small table with width hard-coded to 3 is 'wrong', especially if you look at a 2x5 next to the 4x5:
/* Output:
0 1 2 3 4
9 8 7 6 5
10 11 12 13 14
19 18 17 16 15
0 1 2 3 4
9 8 7 6 5
*/
An alignment mismatch between cells wouldn't manifest until creating a table with over 1000 cells, and that could be considered outside intended use since it would also overflow a standard 80x24 terminal.
Note also that (row, column) means parameters are in (y, x) order, inverse to the more conventional (x, y).
I'm not trying to be annoying, I promise. I'm trying to be helpful since the book is still in beta. Feel free to disregard any or all of the issues I've opened, because they're all minor and subjective.
the course is outdated as hell on jetbrains idea
All chapter have a very shot descriptions about the subjects and till now I found data class
atom the worst and ambiguous in my life.
Chapter 1, Repetition with while, Exrcise1
it asks for printing each character in the string while you didn't teach how to work with index at all before
hi,I'm interested in kotlin language, and your books are really amazing.
This task asks to display every fifth number in a given interval. The example interval is [11..30].
For me, the first fifth member is 11+5=16, and not 15 as the example returns.
An other example:
3 4 5 6 7 8 9 10 11 12 13 14
* *
7 and 12 are returned. 7-3=4, 12-7=5.
There's a typo on page 253 in the first paragraph. The phrase "while expression" should read "when expression". Fantastic book by the way!! Thank you so much for spending the time and making this amazing resource available.
Environment:
IntelliJ IDEA 2018.3 EAP (Ultimate Edition)
Build #IU-183.3283.2, built on October 2, 2018
IntelliJ IDEA EAP User
Expiration date: November 1, 2018
JRE: 1.8.0_152-release-1343-b3 amd64
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
Windows 10 10.0
Plugin: EduTools v2.0-2018.2-906
Kotlin dependency set ext.kotlin_version = '1.2.51' (as 1.3.0 no yet publically available to gradle-plugin)
Gradle set into 4.7 as 4.10 generate cannot run command-line task with issue (idea.log):
2018-10-08 22:01:02,824 [ 70545] WARN - ecker.gradle.GradleCommandLine - #educational: executing :Programming_Basics-String_Templates-Exercise_2:test fails:
Deprecated Gradle features were used in this build, making it incompatible with Gradle 5.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/4.10/userguide/command_line_interface.html#sec:command_line_warnings
When opening the \AtomicKotlin\Programming Basics\String Templates\Exercise 1\src\Task.kt from either project view or learning view it got exception:
kotlin.KotlinNullPointerException
at com.jetbrains.edu.learning.placeholderDependencies.PlaceholderDependencyManager.getReplacementText(PlaceholderDependencyManager.kt:81)
at com.jetbrains.edu.learning.placeholderDependencies.PlaceholderDependencyManager.updateDependentPlaceholders(PlaceholderDependencyManager.kt:51)
at com.jetbrains.edu.learning.editor.EduEditorFactoryListener.editorCreated(EduEditorFactoryListener.java:102)
at sun.reflect.GeneratedMethodAccessor335.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.util.EventDispatcher.dispatchVoidMethod(EventDispatcher.java:133)
at com.intellij.util.EventDispatcher.access$100(EventDispatcher.java:25)
at com.intellij.util.EventDispatcher$3.invoke(EventDispatcher.java:96)
at com.sun.proxy.$Proxy43.editorCreated(Unknown Source)
at com.intellij.openapi.editor.impl.EditorFactoryImpl.createEditor(EditorFactoryImpl.java:208)
at com.intellij.openapi.editor.impl.EditorFactoryImpl.createEditor(EditorFactoryImpl.java:160)
at com.intellij.openapi.fileEditor.impl.text.TextEditorComponent.createEditor(TextEditorComponent.java:144)
at com.intellij.openapi.fileEditor.impl.text.TextEditorComponent.(TextEditorComponent.java:81)
at com.intellij.openapi.fileEditor.impl.text.PsiAwareTextEditorImpl$PsiAwareTextEditorComponent.(PsiAwareTextEditorImpl.java:96)
at com.intellij.openapi.fileEditor.impl.text.PsiAwareTextEditorImpl$PsiAwareTextEditorComponent.(PsiAwareTextEditorImpl.java:89)
at com.intellij.openapi.fileEditor.impl.text.PsiAwareTextEditorImpl.createEditorComponent(PsiAwareTextEditorImpl.java:74)
at com.intellij.openapi.fileEditor.impl.text.TextEditorImpl.(TextEditorImpl.java:56)
at com.intellij.openapi.fileEditor.impl.text.PsiAwareTextEditorImpl.(PsiAwareTextEditorImpl.java:45)
at com.jetbrains.edu.learning.editor.EduEditor.(EduEditor.java:27)
at com.jetbrains.edu.learning.editor.EduFileEditorProvider.createEditor(EduFileEditorProvider.java:29)
at com.intellij.openapi.fileEditor.impl.FileEditorManagerImpl.lambda$openFileImpl4$8(FileEditorManagerImpl.java:858)
at com.intellij.openapi.application.impl.ApplicationImpl.invokeAndWait(ApplicationImpl.java:672)
at com.intellij.openapi.application.impl.ApplicationImpl.invokeAndWait(ApplicationImpl.java:685)
at com.intellij.openapi.fileEditor.impl.FileEditorManagerImpl.openFileImpl4(FileEditorManagerImpl.java:963)
at com.intellij.openapi.fileEditor.impl.FileEditorManagerImpl.openFileImpl3(FileEditorManagerImpl.java:783)
at com.intellij.openapi.fileEditor.impl.FileEditorManagerImpl.lambda$openFileImpl2$4(FileEditorManagerImpl.java:765)
at com.intellij.openapi.command.impl.CoreCommandProcessor.executeCommand(CoreCommandProcessor.java:212)
at com.intellij.openapi.command.impl.CoreCommandProcessor.executeCommand(CoreCommandProcessor.java:178)
at com.intellij.openapi.command.impl.CoreCommandProcessor.executeCommand(CoreCommandProcessor.java:168)
at com.intellij.openapi.command.impl.CoreCommandProcessor.executeCommand(CoreCommandProcessor.java:154)
at com.intellij.openapi.fileEditor.impl.FileEditorManagerImpl.openFileImpl2(FileEditorManagerImpl.java:765)
at com.intellij.openapi.fileEditor.impl.FileEditorManagerImpl.openFileWithProviders(FileEditorManagerImpl.java:696)
at com.intellij.openapi.fileEditor.ex.FileEditorManagerEx.openFile(FileEditorManagerEx.java:145)
at com.intellij.openapi.fileEditor.impl.FileEditorManagerImpl.lambda$openEditor$10(FileEditorManagerImpl.java:1121)
at com.intellij.openapi.command.impl.CoreCommandProcessor.executeCommand(CoreCommandProcessor.java:212)
at com.intellij.openapi.command.impl.CoreCommandProcessor.executeCommand(CoreCommandProcessor.java:178)
at com.intellij.openapi.command.impl.CoreCommandProcessor.executeCommand(CoreCommandProcessor.java:168)
at com.intellij.openapi.command.impl.CoreCommandProcessor.executeCommand(CoreCommandProcessor.java:154)
at com.intellij.openapi.fileEditor.impl.FileEditorManagerImpl.openEditor(FileEditorManagerImpl.java:1119)
at com.intellij.openapi.fileEditor.OpenFileDescriptor.navigateInAnyFileEditor(OpenFileDescriptor.java:152)
at com.intellij.openapi.fileEditor.OpenFileDescriptor.navigateInEditor(OpenFileDescriptor.java:138)
at com.intellij.openapi.fileEditor.OpenFileDescriptor.navigateInEditorOrNativeApp(OpenFileDescriptor.java:134)
at com.intellij.openapi.fileEditor.OpenFileDescriptor.navigate(OpenFileDescriptor.java:117)
at com.intellij.psi.impl.source.PsiFileImpl.navigate(PsiFileImpl.java:892)
at com.intellij.codeInsight.navigation.NavigationUtil.lambda$openFileWithPsiElement$1(NavigationUtil.java:176)
at com.intellij.openapi.command.impl.CoreCommandProcessor.executeCommand(CoreCommandProcessor.java:220)
at com.intellij.openapi.command.impl.CoreCommandProcessor.executeCommand(CoreCommandProcessor.java:178)
at com.intellij.openapi.command.impl.CoreCommandProcessor.executeCommand(CoreCommandProcessor.java:168)
at com.intellij.openapi.command.impl.CoreCommandProcessor.executeCommand(CoreCommandProcessor.java:154)
at com.intellij.codeInsight.navigation.NavigationUtil.openFileWithPsiElement(NavigationUtil.java:169)
at com.intellij.ide.projectView.impl.nodes.AbstractPsiBasedNode.navigate(AbstractPsiBasedNode.java:228)
at com.intellij.ide.projectView.impl.nodes.AbstractPsiBasedNode.navigate(AbstractPsiBasedNode.java:238)
at com.intellij.ide.projectView.impl.nodes.PsiFileNode.navigate(PsiFileNode.java:133)
at com.intellij.util.OpenSourceUtil.navigateToSource(OpenSourceUtil.java:119)
at com.intellij.util.OpenSourceUtil.navigate(OpenSourceUtil.java:74)
at com.intellij.util.OpenSourceUtil.navigate(OpenSourceUtil.java:58)
at com.intellij.util.OpenSourceUtil.navigate(OpenSourceUtil.java:51)
at com.intellij.util.OpenSourceUtil.openSourcesFrom(OpenSourceUtil.java:19)
at com.intellij.util.EditSourceOnDoubleClickHandler$TreeMouseListener.processDoubleClick(EditSourceOnDoubleClickHandler.java:128)
at com.intellij.util.EditSourceOnDoubleClickHandler$TreeMouseListener.onDoubleClick(EditSourceOnDoubleClickHandler.java:120)
at com.intellij.ui.DoubleClickListener.onClick(DoubleClickListener.java:30)
at com.intellij.ui.ClickListener$1.mouseReleased(ClickListener.java:73)
at java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:290)
at java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:289)
at java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:289)
at java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:289)
at java.awt.Component.processMouseEvent(Component.java:6548)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3325)
at com.intellij.ui.treeStructure.Tree.processMouseEvent(Tree.java:410)
at com.intellij.ide.dnd.aware.DnDAwareTree.processMouseEvent(DnDAwareTree.java:58)
at java.awt.Component.processEvent(Component.java:6313)
at java.awt.Container.processEvent(Container.java:2237)
at java.awt.Component.dispatchEventImpl(Component.java:4903)
at java.awt.Container.dispatchEventImpl(Container.java:2295)
at java.awt.Component.dispatchEvent(Component.java:4725)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4889)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4526)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4467)
at java.awt.Container.dispatchEventImpl(Container.java:2281)
at java.awt.Window.dispatchEventImpl(Window.java:2746)
at java.awt.Component.dispatchEvent(Component.java:4725)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:764)
at java.awt.EventQueue.access$500(EventQueue.java:98)
at java.awt.EventQueue$3.run(EventQueue.java:715)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:90)
at java.awt.EventQueue$4.run(EventQueue.java:737)
at java.awt.EventQueue$4.run(EventQueue.java:735)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:734)
at com.intellij.ide.IdeEventQueue.defaultDispatchEvent(IdeEventQueue.java:719)
at com.intellij.ide.IdeEventQueue._dispatchEvent(IdeEventQueue.java:664)
at com.intellij.ide.IdeEventQueue.dispatchEvent(IdeEventQueue.java:363)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
Hi there, I'm trying to run the course in the version of IntelliJ IDEA CE that I just installed (v. 2023.2.1). My Gradle configuration is whatever is default for that version. I have followed all the troubleshooting steps and the problem has not resolved. When I go to check any of the exercises I get the following output:
Problem configuring task :wrapper from command line.
> Unknown command-line option '--tests'.
* Try:
> Run gradle help --task :wrapper to get task usage details.
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.
> Get more help at https://help.gradle.org.
Deprecated Gradle features were used in this build, making it incompatible with Gradle 9.0.
You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins.
For more on this, please refer to https://docs.gradle.org/8.2/userguide/command_line_interface.html#sec:command_line_warnings in the Gradle documentation.
BUILD FAILED in 2s
I'm very frustrated and not sure how to proceed. I mainly bought this book for the IntelliJ integration.
Thanks for your help!
Instead of causing an error, the "list += 3" line successfully inserts another 3 into the list. And the eq test on the next line fails:
[1, 2, 3, 3]
[Error]: [1, 2, 3, 3] != [1, 2, 3]
From the Task Description:
Set breakpoints on lines 4 and 5. [...] Continue the process by clicking on Resume program on the left of the debug window or pressing ⌥⌘R. Notice how the value of the variable x changes after the assignment.
/* 3 */ fun main(args: Array<String>) {
/* 4 */ var x = 1
/* 5 */ x = 2
/* 6 */ println(x)
/* 7 */ }
It seems that if I want to see the value of x
change, I should set breakpoints at lines 5 & 6, rather than at 4 & 5... or just put one at 5 and use the 'Step into' button.
When the debugger stops at line 4, that line has not yet executed, so x
doesn't exist. When it stops at line 5, only line 4 has executed, so x
= 1. Resuming execution with no further breakpoints, we never get to see the value of x
change, since the program immediately finishes execution and terminates.
It would be nice if the AtomicKotlin course could temporarily disable IntelliJ linting in some places. For example, in "var & val" Exercise 1, the "correct" code is:
var answer = 42
answer = 43
...and the linter complains that "Variable 'answer' initializer is redundant." IntelliJ isn't wrong, but it detracts from the clarity of the lesson.
If a business closes at hour 20, and the current hour is 20, the business is closed.
In the following line from Booleans2.kt, the second Boolean test should be <
, not <=
.
val isOpen = hour >= open && hour <= closed
Likewise, in the following line from Booleans3.kt, the second Boolean test should be >=
, not just >
.
val isClosed = hour < open || hour > closed
When trying to buy the full version of the book via Stepik, it says "You cannot buy this course yet."
After I've finished the free sample ("Programming Basics" and "Introduction to Objects") yesterday and tried to buy the full content of the Stepik version, sadly it wasn't possible anymore, with the information "You cannot buy this course yet." on all views where a purchase was normally possible.
The checkSteps() function returns true if the arg is negative. It sould return false.
while
loop given in solution:
while (worker > 0) {
result += worker % 10
worker /= 10
if (worker != 0) {
result *= 10
}
}
The check performed inside the if
is effectively identical to the check performed in the while
loop, so this can be simplified:
while (worker > 0) {
result *= 10
result += worker % 10
worker /= 10
}
The first iteration of result *= 10
performs no unwanted action since at that point result
is still initialized to zero.
Hi, first of all thanks for the book.
I was reading the "Looping and Ranges" section and realised that something may be missing.
After you show the // LoopingAndRanges/HasChar.kt
atom, you say:
The next atom shows that hasChar() is unnecessary—you can use built-in syntax
instead.
But after that text, there is no atom and it follows:
If you simply want to repeat an action a fixed number of times, you may use
repeat() instead of a for loop
And then the // LoopingAndRanges/RepeatHi.kt
atom.
I tried to look for the code in the repository but I can't find it. I don't know if that atom is missing or that bit of text is left over.
Best regards.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.